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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Dingbats
Forum Commoner
Posts: 25
Joined: Fri Dec 05, 2003 10:53 am

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

Post 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? :(
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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.
Dingbats
Forum Commoner
Posts: 25
Joined: Fri Dec 05, 2003 10:53 am

Post by Dingbats »

Thank you, I love you!! At last it works!
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

^^
Post Reply