How to key in only few values into a table in MySQL using PHP?

  • Thread starter Thread starter Haydar
  • Start date Start date
H

Haydar

Guest
I'm trying to enter only some values into a table (MySQL), but it gives me an error. I retrieve data from a form, and I need to store those data into the table.
E,g: $insert = "INSERT INTO anetaret VALUES ('$_POST[idmember]', '$_POST[name]')";
As shown here, I have only two values retrieved from the form.html, but in the table there are more than two fields. I want to send data in only two fields, but it gives me an error. I'm able to send data in every field, but i can't send in only those particular ones. Can anyone give me a hand here, please?

Thank you!
 
I'm pretty sure your syntax is wrong. I'm assuming at this point in your PHP you have already connected to your database using mysql_connect(), so try this... first, save your POST information into some convenient variables -- they can even have the same name:

$idmember = $_POST[idmember];
$name = $_POST[name];

Now, correct me if I'm wrong, but I'm guessing each of the fields in your MySQL table has a name. If your fields were named "field1" and "field2," your code might look like this:

mysql_query("INSERT INTO anetaret
(field1, field2) VALUES( '$idmember', '$name' ) ")
or die(mysql_error());

Try that and see if it works (except use the actual field names, of course). Also, make sure this PHP document is hosted on the same website as your database, because I don't think you can access the database from your computer unless your computer is set up for hosting. And of course make sure your form.html is being directed to the right location.
 
Back
Top