toyfruit Posted September 13, 2008 Share Posted September 13, 2008 Hi all, I'm new to PHP and I can't seem to get my insert statement to work. I want to add some content into a DB table, the variables are retrieved from a form, which I am collecting fine. I haven't included the connection file, but that seems to be working because when I run the query to check the contents of the DB it returns all the values, but never with the new items. I have an auto_increment as the primary key on the table (I assume I only need to list the table rows I want updated)? <?php // check if this file has been posted back to itself with an 'addCategory' action $action = $_GET['action']; $section_id = $_POST['section_id']; $position = 1; $category_name = $_POST['category_name']; $category_desc = $_POST['category_desc']; $URL_link = "gallery.php"; if ($action == 'addCategory'){ // add the new category info to the DB mysql_query("INSERT INTO category (section_id, position, category_name, category_desc, URL_link) VALUES ($section_id , $position, $category_name, $category_desc , $URL_link)"); // echo back a message echo "New Category Added Successfully<br />"; echo $section_id . "<br />"; echo $position . "<br />"; echo $category_name . "<br />"; echo $category_desc . "<br />"; // checking to see if the item has been added to the DB - but it isn't showing $result = mysql_query("SELECT * FROM category", $connection); while ($row = mysql_fetch_array($result)){ echo $row["category_name"] . "<br />"; echo $row["category_desc"] . "<br /><br />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/124057-solved-simple-insert-statement-not-working/ Share on other sites More sharing options...
BlueSkyIS Posted September 13, 2008 Share Posted September 13, 2008 i suggest that you check for SQL errors: mysql_query("INSERT INTO category (section_id, position, category_name, category_desc, URL_link) VALUES ($section_id , $position, $category_name, $category_desc , $URL_link)") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/124057-solved-simple-insert-statement-not-working/#findComment-640455 Share on other sites More sharing options...
toyfruit Posted September 13, 2008 Author Share Posted September 13, 2008 cool - thanks for that, it helped me find out that I need to put '' on the variable values, so it should have been: mysql_query("INSERT INTO category (section_id, position, category_name, category_desc, URL_link) VALUES ('$section_id' , '$position', '$category_name', '$category_desc' , '$URL_link')"); Link to comment https://forums.phpfreaks.com/topic/124057-solved-simple-insert-statement-not-working/#findComment-640460 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.