Question:
PHP inserting dropdown value into SQL database?
Cecily
2009-07-31 09:21:03 UTC
I am trying to insert a value chosen from a drop down menu into my SQL databse.

It doesn't error, it just doesn't work so I have nothing to go off of

//This is my drop down menu//


//This is my code to post it//
$department = $_POST['department'];

$sqlquery =("UPDATE net_users SET department = $department'");
$results = mysql_query($sqlquery);

I have an echo to pull the $department and that works it just doesn't insert it into the table
Four answers:
Vic
2009-07-31 09:49:53 UTC
Try using an INSERT directive instead of an UPDATE one. The UPDATE directive will change all entries in your net_users table to $department since you have not specified a specific key or unique record identifier.... not sure if this is what you want.



If there are no entries (no records to update) in the net_users table then the UPDATE will fail.



Update:

You do not need the "=" sign after the keyword VALUES.







Hope this helps.
Odwin Oddball
2009-07-31 09:31:17 UTC
1. You did not show your SQL connection command or the database selection command. There could be an issue there.



2. You are using UPDATE and not INSERT. This will work if you have existing entries in the table, but its going to fail if there are no entries.



To get more detail on your problem you might want to add this:



echo mysql_error();



this will print out the last error message
?
2016-05-25 03:14:28 UTC
The correct syntax would be mysql_query("INSERT INTO testtable (views, firstname, family, friends) VALUES (20, '$firstname', '$family', '$friends')"); In order to understand why it is this way, remember that MySQL will only receive the variables once they have been resolved to their values. For instance, if $firstname is "John", $family is "Betty", and $friends is "Dave, Tyler, and Nick", then MySQL would read: INSERT INTO testtable (views, firstname, family, friends) VALUES (20, 'John', 'Betty', 'Dave, Tyler, and Nick') Without the quotes, this would be an incorrect query. Also, make sure that the fields are set to VARCHAR or TEXT. TEXT should only be used when the fields are quite large, such as HTML documents or forum posts. If a field is set to INT or a related type (TINYINT, SMALLINT, etc.) then you do not need quotes around the value, in fact, it is recommended that you do not put them.
Fwankie
2009-07-31 14:39:46 UTC
$sqlquery = ("INSERT INTO `net_users` ('department') VALUES ('$department');");



try that, that is the safest way to format the query i think


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...