Forum Moderators: coopster

Message Too Old, No Replies

Problem with MySql. Cannot store data in a table. error in you

         

rus3439

7:15 pm on Aug 22, 2004 (gmt 0)

10+ Year Member



I'm working on php project, but extremely new to it. I'm trying to read from a user and save info in the table and than display it. I created The database and the table in MySql, but cannot store any info in it. Will get a window message saying: You have an error in Sql syntax line 1.
Is it a problem in a set up of MySql. I read on google that the manual of MySql needs to be edited to get rid of this error message. Please advise on how to find this manual or is it the actual problem at all?
Also i get the same messsage when i run php code and and test for errors.
Here is my code:
<?
//establish connection to mysql

// check which button was clicked
// perform calculation
if ($_POST['Submit'])
{
$conn = mysql_connect("localhost","*****","*****") or
die("Could not connect: " . mysql_error());
//select the database
$db = mysql_select_db("data");

$Name = $_POST["Name"];
$Address = $_POST["Address"];
$SS = $_POST["SS#"];
$Birthday = $_POST["Birthday"];
$Birth_Place = $_POST["Birth_place"];
$Comments = $_POST["comments"];
//insert the values into the table
$result= MYSQL_QUERY("INSERT INTO users (Name, Address, SS#, Birthday, Birth_place, comments)".
"VALUES ('$Name', '$Address', '$SS#', '$Birthday', '$Birth_place', '$comments')")or die(mysql_error());

echo "Your Query was succesfully stored in the database :)";

mysql_close($conn);

}

if ($_POST['Show_All_Records']) {


//displaying the database

//establish connection to mysql
$conn = mysql_connect("localhost","rus3439","rus34391") or
die("Could not connect: " . mysql_error());
echo "Connected to MySql".'<br>'.'<br>';
//select the database
$db = mysql_select_db("data");
$sql = "SELECT * FROM `tablename`";
$result = mysql_query($sql) or die(mysql_error());

//display the results
print'Name:'.($_POST["Name"]).'<br>';
print'Address:'.($_POST["Address"]).'<br>';
print'SS#:'.($_POST["SS#"]). '<br>';
print'Birthday:'.($_POST["Birthday"]). '<br>';
print'Place Of Birth:'.($_POST["Birth_place"]). '<br>';
print'Comments:'.($_POST["comments"]).'<br>'.'<br>';

//grabbing all data from the table
while ($r = mysql_fetch_array($result))
{
//the format is $variable = $r["nameofmysqlcolumn"];
$Name=$r["Name"];
$Address=$r["Address"];
$SS=$r["SS#"];
$Birthday=$r["Birthday"];
$Birth_Place=$r["Birth_place"];
$Comments=$r["comments"];
}
echo 'Your Query was succesfully stored in the database :)'
.''.$myrow["Name"].' <br> '.$myrow["Address"].' <br> '.$myrow["SS"].' <br> '.$myrow["Birthday"].' <br> '.$myrow["Birth_place"].' <br> '.$myrow["comments"].' <br>';

mysql_close($conn);

}
?>
And it checks for the errors here:

$result= MYSQL_QUERY("INSERT INTO users (Name, Address, SS#, Birthday, Birth_place, comments)".
"VALUES ('$Name', '$Address', '$SS#', '$Birthday', '$Birth_place', '$comments')")or die(mysql_error());
Thanks a Lot,

[edited by: coopster at 1:33 pm (utc) on Aug. 23, 2004]
[edit reason] generalized username and password [/edit]

templar

11:50 pm on Aug 22, 2004 (gmt 0)

10+ Year Member



Hi,

The error you are getting is because your sql query string is incorrect.

Here is yours:

$result= MYSQL_QUERY("INSERT INTO users (Name, Address, SS#, Birthday, Birth_place, comments)".
"VALUES ('$Name', '$Address', '$SS#', '$Birthday', '$Birth_place', '$comments')")or die(mysql_error());

Here is mine:

$result = mysql_query("INSERT INTO users (Name, Address, SS#, Birthday, Birth_place, comments) VALUES ('$Name', '$Address', '$SS#', '$Birthday', '$Birth_place', '$comments') or die(mysql_error());

You have extra " and . between ) VALUE ( for some reason. I picked up another problem too.

You have:

while ($r = mysql_fetch_array($result))

Should be:

while (($r = mysql_fetch_array($result)))

Note the extra brakets. You need this because you are using an assignment which is always true and the while loop will never end. Putting another set of brackets around will check the value of $r and finish when mysql_fetch_array($result) is done.

cyas
Oliver

rus3439

9:29 am on Aug 23, 2004 (gmt 0)

10+ Year Member



thanks a lot for your suggestion. I just got home from work and dead tired so will test it out tomorrow first thing.

RonPK

11:15 am on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello templar,
Your post triggered me into doing some reading, as I've always used

while($row = mysql_fetch_array($rs))

without any problems. I found this in the manual:

The basic form of a while statement is:

while (expr)
statement


[php.net...]

and, about expressions

Consider the example we've already dealt with, '$a = 5'. It's easy to see that there are two values involved here, the value of the integer constant '5', and the value of $a which is being updated to 5 as well. But the truth is that there's one additional value involved here, and that's the value of the assignment itself. The assignment itself evaluates to the assigned value, in this case 5. In practice, it means that '$a = 5', regardless of what it does, is an expression with the value 5.

[php.net...]

and, about mysql_fetch_array()

Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.

[php.net...]

So, whenever mysql_fetch_array returns false, the whole expression evaluates to false, which will end the while loop. No need for double brackets! Save some bytes ;)