0

我有一个 PHP 脚本,用于更新 MySQL 数据库,我使用它的方式很好,我现在正在使用相同的脚本来尝试更新另一个数据库表,但我无法让它写入任何更新到桌子。该脚本没有显示任何错误,它只是在整个过程中移动,就好像它可以工作一样,但会向 MySQL 发布注释。就像我之前说的,我已经将此脚本与另一种形式一起使用,并且效果很好。我对新表单所做的只是更改变量以匹配我所拥有的并更改数据库表名。我所有的标点符号也是正确的。我只是不明白为什么它不会将更新的信息写入 MySQL。

这是我的代码:

<?php
//----------FORM DATA-----------//
$id=$_POST['id'];

$team_name=!empty($_POST['team_name'])? $_POST['team_name'] : '';
$first_name=!empty($_POST['first_name'])? $_POST['first_name'] : '';
$last_name=!empty($_POST['last_name'])? $_POST['last_name'] : '';
$registration=!empty($_POST['registration'])? $_POST['registration'] : '';
$pay_status=!empty($_POST['pay_status'])? $_POST['pay_status'] : '';
$physical=!empty($_POST['physical'])? $_POST['physical'] : '';
$photo=!empty($_POST['photo'])? $_POST['photo'] : '';
$notes=!empty($_POST['notes'])? $_POST['notes'] : '';


//----------CONNECT TO DATABASE----------//
include 'elite_connect.php';


mysql_query("UPDATE cheer SET  team_name='$team_name', first_name='$first_name', last_name='$last_name', registration='$registration', pay_status='$pay_status', physical='$physical', photo='$photo', notes='$notes'
WHERE `id` = '$id'");

mysql_affected_rows();

echo mysql_error();

?>
<html>
<body style="background-color: #C7DFDF">
<center>
<br><br><br>
<form name="results" method="post" action="cheer_results.php" enctype="multipart/form-data" id="cheerresult">
<input type="submit" class="submit" id="cheerresult" style="width: 165px" value="View Results">
</form>
</center>
</body>
</html>

我非常清楚这可能会导致 sql 注入,所以请不要对此发表评论,在这种情况下我真的不在乎!

4

2 回答 2

1

任何时候你使用$_POST它总是一个好主意检查它是否设置使用isset()

<?php
//----------FORM DATA-----------//

if(isset($_POST['id'])){

   $id=$_POST['id'];

   $team_name=!empty($_POST['team_name'])? $_POST['team_name'] : '';
   $first_name=!empty($_POST['first_name'])? $_POST['first_name'] : '';
   $last_name=!empty($_POST['last_name'])? $_POST['last_name'] : '';
   $registration=!empty($_POST['registration'])? $_POST['registration'] : '';
   $pay_status=!empty($_POST['pay_status'])? $_POST['pay_status'] : '';
   $physical=!empty($_POST['physical'])? $_POST['physical'] : '';
   $photo=!empty($_POST['photo'])? $_POST['photo'] : '';
   $notes=!empty($_POST['notes'])? $_POST['notes'] : '';


   //----------CONNECT TO DATABASE----------//
   include 'elite_connect.php';


   mysql_query("UPDATE cheer SET  team_name='$team_name', first_name='$first_name', last_name='$last_name', registration='$registration', pay_status='$pay_status', physical='$physical', photo='$photo', notes='$notes'
   WHERE `id` = '$id'");

   mysql_affected_rows();

   echo mysql_error();
}
?>

这样,如果您的代码没有执行,您就知道id没有发布。在这种情况下,可以通过将隐藏输入添加到表单中轻松解决。

<input type="hidden" name="id" value="<?php echo $data2['id']?>"  />
于 2013-07-12T06:45:04.940 回答
0

我不清楚,但是,你能试试这个基本语法吗?

mysql_query("UPDATE cheer SET  team_name='".$team_name."', first_name='".$first_name."', last_name='".$last_name."', registration='".$registration."', pay_status='".$pay_status."', physical='".$physical."', photo='".$photo."', notes='".$notes."'
WHERE `id` = '".$id."'");

这对我有用很多次!

于 2013-07-12T06:22:28.077 回答