Add a Car to the Database

connect_errno){ echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } $mysqli->set_charset('utf8mb4'); if(!isset($_POST["carname"])){ /* display the form */ ?>
Car Name:
Image File:
'; $n99problems=false; //check if all form fields were filled if ($carname==''){ print '
  • Name field is empty
  • '; $n99problems=true; } else { print '
  • Uploading' . $carname . '
  • '; } print ''; // get the name of the file on the web server $tmpname = $_FILES['userimage']['tmp_name']; // prepare a statement to do the insertion $stmt = $mysqli->prepare("insert into cars (name, image) values(?,?)"); // bind variables to the statement $blob = NULL; $stmt->bind_param('sb', $carname, $blob); // write the uploaded file to the blob $stmt->send_long_data(1, file_get_contents($tmpname)); $r = $stmt->execute(); if (!$r) { // if false there was an error echo "

    Image could not be uploaded:
    "; echo $mysqli->error; echo "

    "; } else if(!$n99problems) { echo "

    Upload successful

    "; } // we're done! $stmt->close(); $mysqli->close(); } ?>