Advertisement
bu2chlc

add image to database

Nov 5th, 2019
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. <?php
  2. // credentials
  3. $db_user="imageuser";
  4. $db_pass="password";
  5. $db_host="localhost";
  6. $db_name="images";
  7.  
  8. // check for both fields having content
  9. if ( isset ($_POST['title']) && ($_POST['url']) ){
  10.     // assign title and url variables
  11.     $title=$_POST['title'];
  12.     $url=$_POST['url'];
  13.  
  14. // first I define connection string
  15. $mysqli= mysqli_connect($db_host, $db_user, $db_pass, $db_name);
  16. $sql_insert = "INSERT INTO urls (id, title, url) VALUES (NULL, '$title', '$url')";
  17.  
  18. // now make a connection
  19. if($mysqli->connect_errno){
  20.     // if there is a connection error, print the error
  21.     printf("Connect failed: %s\n", $mysqli->connect_error);
  22.     } else {
  23.         // insert image into database:
  24.         if (mysqli_query($mysqli, $sql_insert)) {
  25.             echo "image stored successfully";
  26.         } else {
  27.             printf("Error message: %s\n", mysqli_error($mysqli));
  28.         }
  29.     }
  30. }
  31. ?>
  32.  
  33.  
  34. <form action="" method="post">
  35. <label for="">title</label>
  36. <input type="text" name="title">
  37. <label for="">URL</label>
  38. <input type="text" name="url">
  39. <input type="submit" value="submit">
  40. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement