Page 1 of 1

[SOLVED] What's wrong with this? (MySQL)

Posted: Fri Dec 05, 2003 10:53 am
by Dingbats
I'm trying to learn MySQL. For this page I'm using a db called test, and the only table in it is called test (containing name and favcolor). What's wrong with this:

Code: Select all

<html>
<body bgcolor="f6fff6">
<font face="Verdana" style="font-size: 12px; font-color: #11ff11">
<?php
	$con = mysql_connect("localhost", "root", "");
	$db = mysql_select_db("test");
	
	if(isset($_POST&#1111;'name']) && isset($_POST&#1111;'favcolor']))
	&#123;
	if(isset($_POST&#1111;'name'])) $name_ = $_POST&#1111;'name'];
	if(isset($_POST&#1111;'favcolor'])) $favcolor_ = $_POST&#1111;'favcolor'];
	
	$sql = "SELECT * FROM test WHERE name = ".$_POST&#1111;'name'];
	$result = mysql_query("$sql");
	
	if(!($row = mysql_fetch_array($result))) &#123;
		$sql = "INSERT INTO test (name, favcolor) VALUES ('".$name_."', '".$favcolor_."')";
		$result = mysql_query("$sql");
	&#125;
	&#125;
	
	echo "<table bgcolor='eeffee' width='400' border='1'><tr><td><b>Name</b></td><td><b>Favorite color</b></td></tr>";
	
	$sql = "SELECT * FROM test ORDER BY name";
	$result = mysql_query("$sql");
	
	while($row = mysql_fetch_array($result)) &#123;
		echo "<tr><td>".$row&#1111;'name']."</td><td>".$row&#1111;'favcolor']."</td></tr>";
	&#125;
	
	mysql_close($con);
?>
</table>
<br><br><br><br>
<form method="POST" action="mysql.php">
Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="name"><br>
Favorite color:&nbsp; <input type="text" name="favcolor"><br>
<input type="submit" value="Submit">
</form>
</font>
</body>
</html>
Can you please help a n00b in need? :(

Posted: Fri Dec 05, 2003 11:00 am
by microthick

Code: Select all

$sql = "SELECT * FROM test WHERE name = ".$_POST&#1111;'name'];
   $result = mysql_query("$sql");
This is wrong.

Make sure you encapsulate your $_POST['name'] with quotes as well.

So, if you change it to:

Code: Select all

$sql = "SELECT * FROM test WHERE name = '".$_POST&#1111;'name']."'";
   $result = mysql_query("$sql");
It should work.

Posted: Fri Dec 05, 2003 12:12 pm
by Dingbats
Thank you, I love you!! At last it works!

Posted: Fri Dec 05, 2003 12:15 pm
by microthick
^^