Jump to content

[SOLVED] Simple insert statement not working


toyfruit

Recommended Posts

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 />";
}
}
?>

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());

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')");

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.