0

Here is my code. In this code, when you edit and "update" the data in the database by using PHP, it doesn't change the data in the database or myphpadmin. Take a look at the below code:

<?php
include("dataconn.php"); //connect to database with the external php.

if($_SESSION["loggedin"]!="true")
    header("location:admin_login.php");

$aid=$_SESSION["userid"];
$admin_info="select * from admin where AD_ID='".$aid."'";

    if(isset($_POST["savebtn"]))
{
    $adname=$_POST["name"];
    $adaddress=$_POST["address"];
    $ademail=$_POST["email"];
    $adcontact=$_POST["contact"];

            mysql_query("update admin set AD_NAME='".$ad_name."',ADDRESS='".$adaddress."',EMAIL='".$ademail."',CONTACT_NUM='".$adcontact."' where AD_ID=$aid");

    header("location:profile.php");

}

 ?>

    <body>

        <form name="edit" method="post" action="">
            <tr>
                <th class="title">Name</th>
                <td>:</td>
                <th><input type="text" size="50" value="<?php echo $row["AD_NAME"]?>" name="name"/></th>

            </tr>

            <tr>
                <th class="title">Address</th>
                <td>:</td>
                <th><input type="text" size="50" value="<?php echo $row["ADDRESS"];?>" name="address" /></th>
            </tr>
            <tr>
                <th class="title">Email</th>
                <td>:</td>
                <th><input type="text" size="50" value="<?php echo $row["EMAIL"];?>" name="email"/></th>
            </tr>
            <tr>
                <th class="title">Contact Number</th>
                <td>:</td>
                <th><input type="text" size="50" value="<?php echo $row["CONTACT_NUM"];?>" name="contact"></th>
            </tr>

        <table>

        <span id="edit"><input type="submit" name="savebtn" value="SAVE/CHANGE"/></span>
        </form>


  </body>
   </html>

I have tried to fix this numerous times,but it still has the same problem. Can you help me?

4

3 回答 3

1

为了帮助找出错误:

<?php

echo $adname . '<br />';
echo $adaddress . '<br />';
echo $ademail . '<br />';
echo $adcontact . '<br />';

$result = mysql_query("update admin set AD_NAME='".$ad_name."',ADDRESS='".$adaddress."',EMAIL='".$ademail."',CONTACT_NUM='".$adcontact."' where AD_ID=$aid");

if (!$result) {
    die('Invalid query: ' . mysql_error());
}else{
    //header("location:profile.php");
    echo "Success";
}


?>

并尝试将您的代码更改为 PDO,如下所示:

<?php

if(isset($_POST["savebtn"])){

$adname=$_POST["name"];
$adaddress=$_POST["address"];
$ademail=$_POST["email"];
$adcontact=$_POST["contact"];

try {
  $pdo = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $stmt = $pdo->prepare('UPDATE admin SET AD_NAME=:adname ,ADDRESS = :adaddress , EMAIL = :ademail , CONTACT_NUM = :adcontact WHERE AD_ID = :aid');

  $stmt->execute(array(
    ':adname'   => $adname,
    ':adaddress' => $adaddress,
    ':ademail' => $ademail,
    ':adcontact' => $adcontact,
    ':aid' => $aid
  ));

  header("location:profile.php");

} catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();
}

}

?>
于 2013-07-17T14:10:53.333 回答
1

尝试将您当前的标签替换为我在下面列出的标签,也许会有所帮助。

<form name="edit" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
于 2013-07-17T14:09:21.057 回答
0

您绝对应该考虑迁移到 mysqli 或 PDO 以进行 PHP MYSQL 集成。至少您应该使用至少某种形式的输入转义(即使用 mysql_real_escape_string())。

关于它不起作用,你真的需要让 php/mysql 告诉你它的错误是什么;像这样:

$result = mysql_query("update admin set AD_NAME='".$ad_name."',ADDRESS='".$adaddress."',EMAIL='".$ademail."',CONTACT_NUM='".$adcontact."' where AD_ID=$aid") or die("Error with query: ".$query."<br /> Error message: ".mysql_error());

但是,据说确实能够提供帮助 - 1 错误消息 - 2 表定义

尽管如此,我猜测您的问题可能出在查询的 WHERE 子句中 - 尝试将其作为“...where AD_ID='$aid'”

于 2013-07-17T14:16:20.340 回答