Jump to content

problem inserting and updating


newbreed65

Recommended Posts

Hey Everyone

 

Just making the admin side of my current “project” when I’ve come to a issue inserting and updating data on one of my tables. I’ve spent the last hour looking for what I could be missing but I can’t see what it is, any clues I know it prob something silly.

 

this is the error I’m getting when inserting and it more or less the same with updating

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, dob, bio, modifiedby) VALUES (NULL, 'Morgan', 'Freeman', 'USA', '2005-06-1' at line 1

 

and heres the php file that has the error in

 

<?php
require('../conn.php');

//this code below loops the $_POST Values so $_POST[value] is accessble as $value
foreach ($_POST as $key => $value) {
  $$key = $value;
}

switch ($action) { //$action decides which case "action" (add,edit delete whatever) via what button was clicked on in the first place 
  case "Create Person":

    $sql = "INSERT INTO people (person_id, firstname, surname, from, dob, bio, modifiedby) " .
           "VALUES (NULL, '$firstname', '$surname', '$from', '$dob', '$bio', '$editby')";
    $result = mysql_query($sql) 
      or die(mysql_error());

    $redirect = 'index.php';
    break;

  case "Delete Person":
    $sql = "DELETE FROM people " .
           "WHERE  person_id = $pid";
    $result = mysql_query($sql) 
      or die(mysql_error());

    $sql = "DELETE FROM casts WHERE person_id = $pid";
    $result = mysql_query($sql) 
      or die(mysql_error());

    $redirect = 'index.php';
    break;

  case "Update Person":

    $sql = "UPDATE movie " .
           "SET firstname='$firstname'," . "surname='$surname'," . "from='$from', dob='$dob'," . 
	   "bio='$bio', modifiedby='$editby' " .
           "WHERE person_id = $pid";
    $result = mysql_query($sql) 
      or die(mysql_error());

    $redirect = 'index.php';
    break;
}
header("Location: $redirect");
?>

Link to comment
https://forums.phpfreaks.com/topic/133115-problem-inserting-and-updating/
Share on other sites

    $sql = "INSERT INTO people (`person_id`, `firstname`, `surname`, `from`, `dob`, `bio`, `modifiedby`) " .
           "VALUES (NULL, '$firstname', '$surname', '$from', '$dob', '$bio', '$editby')";

 

I am sure from is a keyword in mysql, use backticks ( ` ) to define columns to avoid this issue.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.