簡體   English   中英

如何更新PHP數據庫中已經存在的記錄

[英]How can i update the records which is already in a database in php

我想更新單選按鈕的值,但它向我顯示消息“出了點問題”(即我的查詢無法執行)。 在提交時,單擊它進入新頁面(即update.php)……請幫助我! 它還顯示未定義索引“ id”的錯誤???

//main.php
<?php
//connecting to the database
$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="company";
@mysql_connect("$db_host","$db_username","$db_pass") or die ("couldnt connect to mysql");
@mysql_select_db("$db_name") or die("no database");

$query = "SELECT driver_name,vehicle_no FROM driver_info"; //You don't need a ; like you do in SQL

$html="";
$rs=mysql_query($query);

                 while($row=mysql_fetch_array($rs))
                 {  
                        $html.='
                        <form action="update.php" method="get">
                        <tr>

                        <td>'.$row['driver_name'].'</td>
                        <td>'.$row['vehicle_no'].'</td>
                        <td><label class="checkbox">
                        <input type="radio" value="free" name="stat" required="required"></label></td>
                        <td><label class="checkbox">
                        <input type="radio" value="travel" name="stat" required="required"></label></td>
                        <td><input type="submit" value="update" name="submit"></td>

                        </tr>
                        </form>';
                 }

?>

//update.php
<?php
include('data_conn.php');

$id=$_GET['id'];
$stat=$_GET['stat'];

$query ="UPDATE driver_info SET status=$stat WHERE id=$id";
$result = mysql_query($query);

    if(!$result) {
                        echo '<script language="javascript">';
                        echo 'alert("something went Wrong...:(((("); location.href="search.php"';
                        echo '</script>';
                }                
                else{
               echo '<script language="javascript">';
               echo 'alert("successfully updated!!!"); location.href="search.php"';
               echo '</script>';
                     }
?>

我認為發生問題是因為您沒有在GET傳遞id 嘗試這個:

//main.php

   <?php
    //connecting to the database
    $db_host="localhost";
    $db_username="root";
    $db_pass="";
    $db_name="company";
    @mysql_connect("$db_host","$db_username","$db_pass") or die ("couldnt connect to mysql");
    @mysql_select_db("$db_name") or die("no database");

    $query = "SELECT id,driver_name,vehicle_no FROM driver_info"; //You don't need a ; like you do in SQL

    $html="";
    $rs=mysql_query($query);

                     while($row=mysql_fetch_array($rs))
                     {  
                            $html.='
                            <form action="update.php" method="get">
                            <input type="text" name="id" value='.$row['id'].' style="display:none"><br>
                            <tr>

                            <td>'.$row['driver_name'].'</td>
                            <td>'.$row['vehicle_no'].'</td>
                            <td><label class="checkbox">
                            <input type="radio" value="free" name="stat" required="required"></label></td>
                            <td><label class="checkbox">
                            <input type="radio" value="travel" name="stat" required="required"></label></td>
                            <td><input type="submit" value="update" name="submit"></td>

                            </tr>
                            </form>';
                     }

    ?>


//update.php
<?php
include('data_conn.php');

$id=$_GET['id'];
$stat=$_GET['stat'];

$query ="UPDATE driver_info SET status=".$stat." WHERE id=".$id";
$result = mysql_query($query);

    if(!$result) {
                        echo '<script language="javascript">';
                        echo 'alert("something went Wrong...:(((("); location.href="search.php"';
                        echo '</script>';
                }                
                else{
               echo '<script language="javascript">';
               echo 'alert("successfully updated!!!"); location.href="search.php"';
               echo '</script>';
                     }
?>

您在哪里獲得$ _GET ['id']? 對我來說,它似乎是空的,所以更新查詢不會執行,因為它沒有ID。

$id=$_GET['id'];

這是空的,沒有分配值,因此查詢將無法工作。

試試這個代碼:

$query ="UPDATE driver_info SET status=".$stat." WHERE id=".$id";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM