简体   繁体   中英

Problem with PHP 'PHP_SELF'

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">
<html xmlns="http://www.w3.org/1999/xhtml">

<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>
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.

You've got a ; after your if statement.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM