Forum Moderators: coopster
// 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]
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
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
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.
and, about mysql_fetch_array()
Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.
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 ;)