Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // session, formatting, etc.
- include('header.php');
- // connect to database, DRY (Dont Repeat Yourself) code
- include('db.php');
- // is there a post?
- if( isset($_POST['title']) AND isset($_POST['url']) )
- {
- if( $_POST['title']<>'' AND $_POST['url']<>'')
- {
- // assign title and url variables
- $title=$_POST['title'];
- $url=$_POST['url'];
- // validation, sanitation
- // is $url an url?... etc.
- // is $title a string?... etc.
- // escaping
- $title=mysqli_real_escape_string($conn,$title);
- $url=mysqli_real_escape_string($conn,$url);
- // the insert query
- $sql_insert = "INSERT INTO urls (id, title, url) VALUES (NULL, '$title', '$url')";
- echo $sql_insert . "<br>";
- // insert image into database:
- if(mysqli_query($conn, $sql_insert))
- {
- echo "string stored successfully";
- } else {
- printf("Error message: %s\n", mysqli_error($conn));
- }
- } else {
- echo 'All the fields are required<br>';
- }
- }
- // show the form.
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Escaped entries</title>
- </head>
- <body>
- </body>
- </html>
- <form action="form.php" method="post" >
- <label for="">title</label>
- <input type="text" name="title" REQUIRED AUTOFOCUS ><br>
- <label for="">URL</label>
- <input type="text" name="url" REQUIRED ><br>
- <input type="submit" value="submit" >
- </form>
Advertisement
Add Comment
Please, Sign In to add comment