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.
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.
<?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);
?>
<?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:
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.
// 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!