View Full Version : mysql php help!

05-25-2011, 06:20 PM
I'm new on mySQL. I want to validate user input from html form with mySQL row.

<form name="login" method="post" action="if.php">
Lucky Number <input type="text" name="guess" />
<input type="submit" value="Submit" />

and if.php is here

$dbhost = "localhost";
$dbname = "gm";
$dbuser = "root";
$dbpass = "";

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

//table rows
$guess = $_POST['guess'];
$result = mysql_query("SELECT guessnum FROM secnum");
$row = mysql_fetch_array( $result);

if($_POST['guess']==$row) {
echo "<p>Good Job!</p>";
} else {
echo "<p>Sorry!</p>";

and my mysql database is like this
Datebase name = gm
row(or column value)=1

05-25-2011, 06:32 PM
if($_POST['guess']==$row) {

That won't work because $row is an array. But more to the point, you should be able to use a WHERE clause on your query to determine if your row exists, rather than selecting _every row_ in your table and then using PHP to look for the row you're after.

Look in the PHP manual at php.net for examples of how to use mysql_fetch_array(). Also look for some basic SQL tutorials on the internet.