0

I'm new to mysql. Could anyone please tell me whats wrong with this code. It connects to the database but doesnt insert the data.

When i press sumbit, this comes up... "you have been connected successfullyError Inserting New Record"

<?php


DEFINE ('DB_HOST', 'localhost'); // Host name
DEFINE ('DB_USER', '');
DEFINE ('DB_PSWD', '');
DEFINE ('DB_NAME', '');


$dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);

if (!$dbcon) {
    die('Error Connecting to Database');
    }
echo 'you have been connected successfully';


   $title = $_POST['title'];
   $first_name = $_POST['first_name'];
   // $surname = $_POST ['surname'];
   // $gender = $_POST ['gender'];
   // $dob = $_POST ['dob'];
   // $street_address = $_POST ['street_address'];
   // $suburb = $_POST ['suburb'];
   // $state = $_POST ['state'];
   // $home_phone = $_POST ['home_phone'];
   // $mobile_phone = $_POST ['mobile_phone'];
   // $work_phone = $_POST ['work_phone'];
   // $email = $_POST ['email'];
   // $martial_status = $_POST ['martial_status'];
   // $occupation = $_POST ['occupation'];
   // $parent_name = $_POST ['parent_name'];
   // $preferred_dr = $_POST ['preferred_dr'];
   // $medicare_no = $_POST ['medicare_no'];

   $sqlinsert = "INSERT INTO Patient Details Table (Title,First Name) VALUES ('$title', '$first_name')";

if (!mysqli_query($dbcon, $sqlinsert)) {
    die('Error Inserting New Record');
    }
else
{ 
    echo 'New Record Inserted';
}




?>
  • What are the values you're trying to enter? Whats your column name really called for `First Name`? – Lawrence Cherone Sep 01 '17 at 01:01
  • 2
    You're removing any error message and replacing it with your own generic message. Stop doing that, and display the actual error message so you'll know what went wrong. When you throw away all of the error information, you can't figure out what the error was or what causes it. Your current code says *If something goes wrong, give me this useless message "Error inserting new record" instead of anything useful*. When you cut off your own head, don't be surprised when you have difficulty using your brain. – Ken White Sep 01 '17 at 01:02
  • The space in `First Name` is your problem, I think, backticks `\`First Name\`` might help? But I agree with Ken White. – KIKO Software Sep 01 '17 at 01:06
  • Also look at https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Lawrence Cherone Sep 01 '17 at 01:12
  • Ok thanks, although how would I go about just making it show the problem by taking out the "Error inserting new record". Also i put `First Name` and the problem still consists – D. Danny Sep 01 '17 at 01:18

0 Answers0