Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Problem with INSERT statement

Explorer ,
Jul 10, 2011 Jul 10, 2011

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

TOPICS
Server side applications

Views

470
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 10, 2011 Jul 10, 2011

Copy link to clipboard

Copied

you have:

or die(mysqli_error());

should be:

or die(mysql_error());

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 10, 2011 Jul 10, 2011

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!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 10, 2011 Jul 10, 2011

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 10, 2011 Jul 10, 2011

Copy link to clipboard

Copied

Hi

Now getting this error

Incorrect date value: '0' for column 'date' at row 1

Maxwellmb

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 10, 2011 Jul 10, 2011

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 11, 2011 Jul 11, 2011

Copy link to clipboard

Copied

LATEST

glad its sorted. you can mark post as resolved.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines