tags:

views:

50

answers:

4

I am having a bit of trouble. It does not seem to be a big deal, but I have created a page that the user can edit a MySQL database. Once they click submit it should process the php within the if statement and echo 1 record updated. The problem is that it does not wait to echo the statement. It just seems to ignore the way I wrote my if and display the whole page. Can anyone see where I went wrong.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php require("serverInfo.php"); ?>
<?php
    $res = mysql_query("SELECT * FROM cardLists order by cardID") or die(mysql_error()); 
    echo "<select name = 'Cards'>"; 
    while($row=mysql_fetch_assoc($res)) { 
        echo "<option value=\"$row[cardID]\">$row[cardID]</option>"; 
    } 
    echo "</select>";
?>
Amount to Add: <input type="text" name="Add" />
<input type="submit" />
</form>

<?php
if(isset($_POST['submit']));
{
    require("serverInfo.php");
mysql_query("UPDATE `cardLists` SET `AmountLeft` = `AmountLeft` + ".mysql_real_escape_string($_POST['Add'])." WHERE `cardID` = '".mysql_real_escape_string($_POST['Cards'])."'");
 mysql_close($link);
 echo "1 record Updated";
}
?>
<br />
<a href="index.php"> <input type="submit" name="main" id="main" value="Return To Main" /></a>
</body>
</html>
A: 

Of course it doesnt. PHP runs in the server side, not in browser! Open your page source. There is no PHP. Nothing to wait. You need another page to send your form to.

And it is a big deal. It's a cornestone of understanding how the web does work.

Col. Shrapnel
Why do I need another page to send my source to? In the book I was using they suggested using this way for small scripts?
shinjuo
PHP_SELF with put the current page into the action of the form so when you submit, it will load the same page and process your posted data. Where you process the data does not matter. It's perfectly normal to see PHP in the middle of HTML like that, I do it all the time.
animuson
+4  A: 
if(isset($_POST['submit']));

1) Should not have a semicolon after it.

2) $_POST['submit'] is not set. You have to set a name on your submit button and give it a value. Just setting the type to 'submit' does not return a value for $_POST['submit'] in PHP.

animuson
In order for `$_POST['submit']` to be set you should put a `name` attribute on your `<input>`, like so: `<input type="submit" name="submit" />`
Felix
Thank you this helps a lot
shinjuo
Unless you have multiple submit buttons and need to differentiate actions based on which button was pressed, it's always better to check `$_SERVER['REQUEST_METHOD'] == 'POST'` rather than for the presence of some particular POST variable. It might not be there, but the request_method always will be.
Marc B
You use that instead of the $_POST['submit'] right?
shinjuo
+2  A: 

You've got a ; after your if statement.

jackbot
Thank you this helps
shinjuo
This is precisely why I never use the Allman brace style if I can help it...
eswald
A: 

I noticed that you have two submit buttons and I assume that you are using the first one.

Try giving it a name="submit" and a value too.

Jonathan Czitkovics
I only see one submit button?
shinjuo