Rkboss
Rkboss

Reputation: 19

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

I want to update the radio button value, but it shows me message "something went wrong"(i.e. my query doesn't get execute). on submit click it went to a new page (i.e update.php)......please help me! it also shows an error of undefined index '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>';
                     }
?>

Upvotes: 0

Views: 50

Answers (3)

Akshay
Akshay

Reputation: 710

I think problem occurs because you are not passing id in GET. Try this:

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

Upvotes: 1

bocha lee
bocha lee

Reputation: 1

Try this code:

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

Upvotes: 0

kerv
kerv

Reputation: 315

Where do you get $_GET['id']? To me it seems that its empty so the update query wont execute because it has no ID.

$id=$_GET['id'];

this is empty and has no value assigned so the query won't work.

Upvotes: 0

Related Questions