Insert data into table- simple problem

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
fuzzyfuzz
Forum Newbie
Posts: 10
Joined: Fri Dec 05, 2008 5:07 pm

Insert data into table- simple problem

Post by fuzzyfuzz »


Hi!
I posted the data in this script from a form - the variables in this code stored the data inserted from the textboxes in the form.
from some reason this doesn't work.

Any Idea?

Code: Select all

 
 
<?php
$x = $_POST['name'];
$y = $_POST['age'];
$z= $_POST['subject'];
 
$mysql_link= mysql_connect('localhost','root','') or die("ERROR:cannot connect");
echo "connected successfully to MySQL server.";
 
mysql_select_db('pets',$mysql_link) or die ("could not open db".mysql_error());
echo "connected successfully to pets.";
 
mysql_query("INSERT INTO students (name, age, subject) 
VALUES ($x, $y, $z)"); 
mysql_close($mysql_link);
?>
 
 
SteveC
Forum Commoner
Posts: 44
Joined: Thu Dec 04, 2008 2:39 pm
Location: Lansing, MI

Re: Insert data into table- simple problem

Post by SteveC »

Code: Select all

 
 
<?php
$x = $_POST['name'];
$y = $_POST['age'];
$z= $_POST['subject'];
 
$mysql_link= mysql_connect('localhost','root','') or die("ERROR:cannot connect");
echo "connected successfully to MySQL server.";
 
mysql_select_db('pets',$mysql_link) or die ("could not open db".mysql_error());
echo "connected successfully to pets.";
 
mysql_query("INSERT INTO students (name, age, subject) 
VALUES ($x, $y, $z)"); 
mysql_close($mysql_link);
?>
 
 
Yes, you need to encase your values with quotes or double quotes, what you have here is wrong:

Code: Select all

INSERT INTO students (name, age, subject) 
VALUES ($x, $y, $z)
You need:

Code: Select all

INSERT INTO students (name, age, subject) 
VALUES ('$x', '$y', '$z')
You don't necessarily need quotes around the age, but it certainly doesn't hurt.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Insert data into table- simple problem

Post by jaoudestudios »

Do you get any errors? or does it just not add the info to the database?

Can you post your database schema!
cavemaneca
Forum Commoner
Posts: 59
Joined: Sat Dec 13, 2008 2:16 am

Re: Insert data into table- simple problem

Post by cavemaneca »

SteveC wrote:

Code: Select all

INSERT INTO students (name, age, subject) 
VALUES ('$x', '$y', '$z')
Does this really work? I would do that script something like this.

Code: Select all

$query = "INSERT INTO `students` (`name`, `age`, `subject`) 
           VALUES ('".$x."', '".$y."', '".$z."')";
$result = mysql_query($query);
I like it this way because I have them separated and also I can check the results. Mainly, It just helps me debug. But I didn't know you could make it smoother, like one line, and have it still look good.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Insert data into table- simple problem

Post by jaoudestudios »

An even better way is to be more precise...

Code: Select all

 
// mysql
INSERT INTO students SET name = '$name', age = '$age', subject = '$subject'
 
// php
$q = "INSERT INTO students SET name = '".$name."', age = '".$age."', subject = '".$subject."'";
 
Yes to your question cavemaneca, it is possible to put the query straight into mysql_query() - but it is lazy. Plus when queries become more complicated like 5 to 10 lines long it makes it more difficult to follow!
Post Reply