Jump to content

Message edit error


star_commander
 Share

Recommended Posts

I am writing a script to allow editing of a message once a user name and password have been provided. The script should display a message from the database and have a text box below for a new message. Whenever I run this, I get the textbox and the message, but when I click submit, I get the "Please fill in required info error" that I had put in the script. The script goes backwards.

<!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>Edit</title></head><body><?php$host = "localhost";$user = "test";$pass = "test";$db = "test";$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");mysql_select_db($db) or die ("Unable to select database!");$id = $_POST['id'];$vid = $_POST['vid'];$vids = mysql_query("SELECT * FROM testWHERE id='$id'") or die ("Wrong View id.");$row = mysql_fetch_array($vids) or die ("Please fill in required info.");$comp = $row['vid'];$you = $row['memo'];if($vid==$comp){	if (!isset($_POST['submit'])) {		?>        Your Message:        <?php		echo "$you";		?>		<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">  		<label>  		<input type="text" name="edit" id="edit" />  		</label>  		<label>  		<input type="submit" name="submit" id="submit" value="Edit" />  		</label>		</form>		<?php	}	else {		$edit = $_POST['edit'];		mysql_query("DELETE FROM test WHERE id='$id'");		mysql_query("INSERT INTO test (vid, id, edit)		VALUES ('$vid', '$id', '$memo')") or die ("ID has already been taken.");		echo "Success";	}}else{echo "Wrong Edit ID";}?></body></html>

Link to comment
Share on other sites

It will still work. When the result set is empty then mysql_fetch_* will return false, and when it does that then the script will die. If the first function in a statement like that returns false then the script will die, if it returns a non-false value then the script won't die. You can use it like that with any statement that will evaluate to true or false.

Whenever I run this, I get the textbox and the message, but when I click submit, I get the "Please fill in required info error" that I had put in the script.
You'll only see that error if the query was a valid query but it didn't return any records. One thing you're not doing is checking if the form was submitted before querying the database. If $_POST['id'] isn't set then it won't return any records (presumably) and you'll see that message.
Link to comment
Share on other sites

I deleted the "or die" statement after the mysql_fetch_array() and I changed

mysql_query("INSERT INTO test (vid, id, edit)VALUES ('$vid', '$id', '$memo')") or die ("ID has already been taken.");

to

mysql_query("INSERT INTO test (vid, id, memo)		VALUES ('$vid', '$id', '$edit')") or die ("ID has already been taken.");

I get the success message, but the variables I had set earlier "$id" and "$vid" are not being passed to the query, so only the message "$edit" is being stored.

Link to comment
Share on other sites

It doesn't make sense that the variables would be set in one place and all of a sudden not be set any more without anything happening. Computers don't work like that. You can try to print the globals array and see what everything is set to, but if you set the variables here:$id = $_POST['id'];$vid = $_POST['vid'];Then they aren't just going to all of a sudden lose their values. If the code you posted isn't the code you're using then post what you're using. Use this and then view the source of your page to check on variables:print_r($GLOBALS);

Link to comment
Share on other sites

<!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>Edit</title></head><body><?php$host = "localhost";$user = "test";$pass = "test";$db = "test";$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");mysql_select_db($db) or die ("Unable to select database!");$id = $_POST['id'];$vid = $_POST['vid'];$vids = mysql_query("SELECT * FROM testWHERE id='$id'") or die ("Wrong View id.");$row = mysql_fetch_array($vids);$comp = $row['vid'];$you = $row['memo'];if($vid==$comp){	if (!isset($_POST['submit'])) {		?>        Your Message:        <?php		echo "$you";		?>		<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">  		<label>  		<input type="text" name="edit" id="edit" />  		</label>  		<label>  		<input type="submit" name="submit" id="submit" value="Edit" />  		</label>		</form>		<?php	}	else {		$edit = $_POST['edit'];		echo "$you";		mysql_query("DELETE FROM test WHERE id='$id'");		mysql_query("INSERT INTO test (vid, id, memo)		VALUES ('$vid', '$id', '$edit')") or die ("ID has already been taken.");		echo "Success!";		print_r($GLOBALS);	}}else{echo "Wrong Edit ID";}?></body></html>

That's the code I'm using.

[db] => test [connection] => Resource id #2 [id] => [vid] => [vids] => Resource id #3 [row] => [comp] => [you] => [edit] => test )

That's the last line of the print_r($GLOBALS) command, where the $id and $vid variables are stored.

Link to comment
Share on other sites

It sounds like they aren't even being submitted to the page, in which case you probably have errors showing up that you're not aware of. Add these lines to the top of the script:ini_set("display_errors", 1);error_reporting(E_ALL);

Link to comment
Share on other sites

Notice: Undefined index: id in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\finalproject\test5.php on line 18Notice: Undefined index: vid in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\finalproject\test5.php on line 19

The values would have to exist for the test at the first 'if' statement. The 'if' statement works, for if I type in the wrong vid, I am taken to the end of the script. The values seem to disappear somewhere between the first and the second 'if' statement.

Link to comment
Share on other sites

Variables don't disappear, you need to trust that the computer is working correctly and that the problem is in your code. The variables aren't being set at all, those 2 error messages mean that id and vid are not found in $_POST. The reason the if statement succeeds is because $comp isn't set either, so you're comparing two blank variables, which obviously match. That's why the first if statement succeeds, not because $vid is set correctly. Since we know that id and vid are not in $_POST, look what's happening here:

$id = $_POST['id'];  // set to empty string$vid = $_POST['vid'];  // set to empty string$vids = mysql_query("SELECT * FROM test WHERE id='$id'") or die ("Wrong View id.");  // returns empty result set (empty resource)$row = mysql_fetch_array($vids);  // returns false ($row === false)$comp = $row['vid'];  // set to empty string$you = $row['memo'];  // set to empty stringif($vid==$comp)  // compare 2 empty strings

Scroll back up and look at the output from print_r($GLOBALS). $row, $comp, and $you are all empty strings, or false, in addition to $id and $vid. So you aren't even getting the values from $_POST in the first place because they aren't being submitted. Make sure that whatever form is submitting to this page is submitting the correct values. You can use print_r($_POST) to see everything that got submitted, or $_POST will also show up in the output for print_r($GLOBALS).

Link to comment
Share on other sites

Thanks :)I put print_r($_POST) at the top of the code and got this as a result.

Array ( [vid] => 4321 [id] => 1234 [button] => Edit! )

I tried placing print_r($GLOBALS) in various places around my script. It printed the correct variables everywhere but the second 'else' statement after the first two 'if' statements. There, new variables are stored, but they shouldn't overwrite the old ones.

Link to comment
Share on other sites

If they have the same name then they will overwrite. $_POST should never change though, you're never assigning anything to $_POST in that code. There's only about 30 lines of PHP there, there's not a lot going on. The reason I thought that the values weren't getting submitted is because the form that you print if $_POST['submit'] is not set (which it's not set in the output you pasted above) does not have fields for id and vid. It has a field called "edit" and a submit button.

Link to comment
Share on other sites

<?php$host = "localhost";$user = "test";$pass = "test";$db = "test";$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");mysql_select_db($db) or die ("Unable to select database!");$id = $_POST['id'];$vid = $_POST['vid'];$vids = mysql_query("SELECT * FROM testWHERE id='$id'") or die ("Wrong View id.");$row = mysql_fetch_array($vids);$comp = $row['vid'];$you = $row['memo'];if($vid==$comp){	if (!isset($_POST['submit']))	{		?>		Your Message:		<?php		echo "$you";		?>		<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">		  <label>		  <input type="text" name="edit" id="edit" />		  </label>		  <label>		  <input type="submit" name="submit" id="submit" value="Edit" />		  </label>		</form>		<?php	}	else {		$edit = $_POST['edit'];		mysql_query("DELETE FROM test WHERE id='$id'");		mysql_query("INSERT INTO test (vid, id, memo)		VALUES ('$vid', '$id', '$edit')") or die ("ID has already been taken.");		echo "Success!";	}}else{echo "Wrong Edit ID";}?>

I don't think it's changed.

Link to comment
Share on other sites

That code runs for me exactly like I would expect it to. For me, the first time I load the page I get a form with a text field and submit button. I hit submit and get a message that says "success". There are several things wrong with the logic in the script, but it works like the code says it should, and the values in $_POST never change. One thing you're not doing on the top of the page is checking if anything has even been submitted, you're just querying the database with a bunch of blank values. We've already talked about that in this thread though. You're also using the "or die" construct incorrectly, when you use it on a query like this:$vids = mysql_query("SELECT * FROM test WHERE id='$id'") or die ("Wrong View id.");it's *only* going to show that message if the query failed, i.e. if mysql_query returns a false value. If the id just wasn't found, you're not going to see that message. If you want to check if the ID was found then you need to check how many rows were returned.

$vids = mysql_query("SELECT * FROM test WHERE id='$id'");if (mysql_num_rows($vids) == 0)  die("Wrong view id");

The same thing goes with the insert statement. Instead of doing the insert and checking if an error happened, it would be better for your users if you checked if the ID was taken first and show an error message if so, or else do the insert.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...