Copy link to clipboard
Copied
Hi
I'm trying to insert data into a database via a form.
The form has a drop down menu called $name
and a textarea called $report
I'm sure my problem has something to do with the INSERT statement syntax.
Can someone help with this please?
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$report = $_POST['report'];
$output_form = 'no';
if (empty($name) || empty($report)) {
echo 'Please fill in all fields.<br />';
$output_form = 'yes';
}
}
else {
$output_form = 'yes';
}
if (!empty($name) && !empty($report)) {
$dbc = mysqli_connect('127.0.0.1', 'root', 'root', 'abe')
or die('Error connecting to MySQL server.');
date_default_timezone_set('Europe/London');
$date = date('Y'-'m'-'d');
$query = ("INSERT INTO reports VALUES ('', '$name', '$report', '$date')") or die(mysqli_error());
mysqli_query($dbc, $query)
or die ('Data not inserted.');
echo 'data added.';
mysqli_close($dbc);
}
Best regards maxwellmb
Copy link to clipboard
Copied
you have:
or die(mysqli_error());
should be:
or die(mysql_error());
Copy link to clipboard
Copied
Hi Thanks for the help!
I tried
or die(mysql_error());
still says Data not inserted
I tried changing the statement to
$query = "INSERT INTO reports (id, name, report, date)VALUES ('NULL','".$name."','".$report."','".$date."')";
mysqli_query($dbc, $query) or die ('Error updating database');
and it still says Data not inserted!
Copy link to clipboard
Copied
$query = "INSERT INTO reports (id, name, report, date)VALUES ('NULL','".$name."','".$report."','".$date."')";
mysqli_query($dbc, $query) or die ('Error updating database');
try:
$query = mysql_query("INSERT INTO reports (name, report, date) VALUES ('$name', '$report, '$date')")
or die(mysql_error());
other columns should default to null
Copy link to clipboard
Copied
Hi
Now getting this error
Incorrect date value: '0' for column 'date' at row 1
Maxwellmb
Copy link to clipboard
Copied
Hi
I changed my database date field to
timestamp on update CURRENT_TIMESTAMP No CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
and set date to null in my INSERT statement
$query = mysql_query("INSERT INTO reports (name, report, date) VALUES ('$name', '$report', null)") or die(mysql_error());
The information now inserts into the database as required.
Many thanks!
Maxwellmb
Copy link to clipboard
Copied
glad its sorted. you can mark post as resolved.