如何在php中更新已存在于数据库中的记录

时间:2016-07-05 09:36:05

标签: php mysql

我想更新单选按钮值,但它显示消息“出错了”(即我的查询没有执行)。在提交点击它去了一个新的页面(即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>';
                     }
?>

3 个答案:

答案 0 :(得分:1)

我认为问题出现是因为您没有在id中传递GET。 试试这个:

// 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>';
                     }
?>

答案 1 :(得分:0)

你在哪里获得$ _GET [&#39; id&#39;]?对我来说,似乎它是空的,因此更新查询不会执行,因为它没有ID。

$id=$_GET['id'];

这是空的,没有分配值,因此查询无法正常工作。

答案 2 :(得分:0)

试试这段代码:

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