www.webdeveloper.com
Results 1 to 2 of 2

Thread: why could not be able to update...

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Posts
    57

    why could not be able to update...

    hi, I tried to change update query from mysql to mysqli, at first under mysql is works great to update into mysql database but when i changed to mysqli is not doing update it. can anyone tell me what did i missed!

    under mysql

    PHP Code:
    <?php
    include('db.php');
    if(isset(
    $_GET['status']))
    {
    $status1=$_GET['status'];
    $select=mysql_query("select * from manage where id='$status1'");
    while(
    $row=mysql_fetch_object($select))
    {
    $status_var=$row->status;
    if(
    $status_var=='0')
    {
    $status_state=1;
    }
    else
    {
    $status_state=0;
    }
    $update=mysql_query("update manage set status='$status_state' where id='$status1' ");
    if(
    $update)
    {
    header("Location:index.php");
    }
    else
    {
    echo 
    mysql_error();
    }
    }
    ?>
    <?php
    }
    ?>

    and under mysqli

    PHP Code:
    <?php
    include('db.php');
    $dbcs = new mysqli($mysql_hostname$mysql_user$mysql_password$mysql_database);
    if(isset(
    $_GET['status']))
    {
    $status1=$_GET['status'];

      
    $sql "select * from product where product_id='$status1'";
      
    $result=mysqli_query($dbcs,$sql);

    while(
    $row=mysqli_fetch_object($result))
    {
    $status_var=$row->status;
    if(
    $status_var=='0')
    {
    $status_state=1;
    }
    else
    {
    $status_state=0;
    }

    $mysqli->query("UPDATE product set status='$status_state' where product_id='$status1' ");
    if(
    $mysqli)
    {
    header("Location:item.php");
    }
    else
    {
    echo 
    mysqli_error();
    }
    }
    ?>
    <?php
    }
    ?>
    Last edited by Alidad; 01-03-2014 at 08:38 PM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,392
    First problem I see is that you instantiate your MySQLi object as $dbcs, but then later try to use the undefined $mysqli variable.

    In general, it's a good idea to check the return values from query calls and such to see if they're false, so that you can log/output some debug info at that point.

    Also, using $_GET (or other external) values directly in your SQL is asking for trouble. Since you're moving up to the world of MySQLi, why not use prepared statements with bound parameters, so that you don't have to worry about SQL injection errors/attacks?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles