user1187135
user1187135

Reputation:

Is this the right way to insert into mysql table using PHP?

I want to know if this is correct for inserting into table in step 3 using mysql_query. Also, how do I ensure it is inserted into the right row? I am guessing I am suppose to use the WHERE statement. Do I do this: INSERT....WHERE.... VALUES

<?php 
if (isset($_POST)){
//1. Define form variables
$language = $_POST['language'];
$level = $_POST['level'];
$language_learn = $_POST['language_learn'];
$learn_level = $_POST['learn_level'];
$preferred_contact = $_POST['preferred_contact'];
$city = $_POST['city'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$description = $_POST['description'];

//2. Check if form variables are valid

//3. Insert into table
$insert = mysql_query("
    INSERT INTO user (language, level, language learn, learn_level, preferred_contact, city, age, sex, description) 

    VALUES ('".$language."', '".$level."', '"$language_learn."', '".$learn_level."', '".$preferred_contact."', '".$city."', '".$age."', '".$sex."', ".$description."')");

//4. Reload page at home page 

}//end $_POST
?>

Upvotes: 1

Views: 131

Answers (5)

Songo
Songo

Reputation: 5736

First of all you must insure that you have a primary key in your table. You probably have a column like user_id in your table definition that is the primary key for that table.

Second you should be setting that column as auto increment so whenever you add a new record to your table the value for the user_id will be added automatically.

CREATE TABLE 'user'
(
'user_id' int(11) NOT NULL AUTO_INCREMENT,
         .........
         other columns
         .........
PRIMARY KEY ('user_id')
) ENGINE=INNODB;

If you have a primary key, but you didn't set it to auto increment then you must always insert its value whenever you issue an insert statement. So basically this will fail as NULL is not an allowed value for a primary key.

$insert = mysql_query("
INSERT INTO user (language, level, language learn, learn_level, preferred_contact, city, age, sex, description) 

VALUES ('{$language}', '{$level}', '{$language_learn}', '{$learn_level}', '{$preferred_contact}', '{$city}', '{$age}', '{$sex}', {$description}')");

Other than that your query should work fine :)

Note: You should use parameterized queries.

Upvotes: 0

Balaji Kandasamy
Balaji Kandasamy

Reputation: 4506

You can make ensure of insertion by use of mysql_insert_id(),

eg: after mysql_query(insert query) u can use like this.

if(mysql_insert_id >0)
{
echo "Data inserted successfully";
}

Upvotes: 0

Rafee
Rafee

Reputation: 4078

And you can use it directly without giving " and . for each variable

$insert = mysql_query("
                         INSERT INTO user 
                         (language, level, language learn, learn_level, preferred_contact, city, age, sex, description) 
                          VALUES 
                         ('$language', '$level', '$language_learn', '$learn_level', '$preferred_contact', '$city', '$age', '$sex','$description.')
                    ");

Upvotes: 0

heyanshukla
heyanshukla

Reputation: 669

It seems correct. And where is used to select/update/delete/etc.... and not for insertion.

Upvotes: 0

yehuda
yehuda

Reputation: 1282

Insert automatically creates a new row with a new increment ID assuming your table is set up correctly. There is no WHERE in an INSERT Statement, only in an update statement. And you should enclose all your POSTs with mysql_real_escape_string() for security.

Upvotes: 2

Related Questions