dongxun7962 2013-07-17 14:00
浏览 22
已采纳

为什么我不能使用PHP编辑/更新数据库?

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?

  • 写回答

3条回答 默认 最新

  • dongmacheng3222 2013-07-17 14:10
    关注

    To help finfing the error:

    <?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";
    }
    
    
    ?>
    

    And try to change your code to PDO, something like this:

    <?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();
    }
    
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决