I want to update a table based on what a user enters. I know I got the username and password correct. But I keep getting an error based on my debugging code. I'm connecting to the database successfully, but it's just not updating. The variables print into the page, so they're not Null. What could be causing my table not to update? If this helps, I'm using Yahoo Web hosting to do this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Posted</title>
</head>

<body>


<?

	$name = $_REQUEST["name"];
	$date = $_REQUEST["date"];
	$make = $_REQUEST["make"];
	$model = $_REQUEST["model"];
	$year = $_REQUEST["year"];
	$rating = $_REQUEST["rating"];
	$service = $_REQUEST["service"];
	
	$conn = mysql_connect("mysql", "my_name", "my_password");
	$select = mysql_select_db("Posts");
	$sql = "INSERT INTO posts VALUES (NULL, \'$name\', \'$date\', \'$make\', \'$model\', \'$year\', \'$service\', \'$rating\')'";
	
	$result = mysql_query($sql);

        print "Name: " . $name . " Date: " .$date . " Make: " . $make . " Model: " . $model . " Year: " . $year . " Rating: " . $rating . " Service: " . $service . "\n\n\n\n\n\n\n\n\n";

        if($conn){
		print "Connected to database successfully";
	}else{
		print "Error!";
	}
	
	if($result){
		print "Database updated";
	}else{
		print "Error!";
	}
	
?>
</body>
</html>

Recommended Answers

All 2 Replies

I got the error corrected, but now it's just posting $name, $date, etc. into my database.

i revised your code and it look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Posted</title>
</head>

<body>


<?

	$name = $_REQUEST["name"];
	$date = $_REQUEST["date"];
	$make = $_REQUEST["make"];
	$model = $_REQUEST["model"];
	$year = $_REQUEST["year"];
	$rating = $_REQUEST["rating"];
	$service = $_REQUEST["service"];
	
	$PRIMARYKEY = $_REQUEST["ID"];
	
	
	$conn = mysql_connect("mysql", "my_name", "my_password") or die(mysql_error());
	mysql_select_db("Posts") or die(mysql_error());
	$sql = "UPDATE post 
			SET field1 = ".$name.", field2 = ".$make.", 
			field3 = ".$model.", field4 = ".$year.", 
			field5 = ".$service.", field6 = ".$rating." WHERE fieldID = ".$PRIMARYKEY;
		
	mysql_query($sql) or die(mysql_error());

	echo "Name: ".$name." Date: ".$date." Make: ".$make." Model: ".$model." Year: ".$year." Rating: ".$rating." Service: ".$service."<br /><br />";
	
	echo "Database Updated!";
	
?>
</body>
</html>

this code should work, I tested it in my local server and it work just fine.
Do not use "INSERT" when updating a record, use "UPDATE" instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.