简体   繁体   中英

Update fields from MySQL using php form

I have been writting a script to update fields in MySQL from a form using php.

I have an issue, when I try to update the field since php using mysqli, there are no changes on my database. Here is the code, I use 2 pages. Search.php and Update.php .

This code show the result of my search, and works well.

Search.php

$searchValue=$_GET["txtSearch"]; 

    $connection=mysqli_connect($db_host, $db_user, $db_password, $db_name);
    $query="select UserID, LastName, FirstName, UserName, Phone from User where UserID='$searchValue'";
    $recordset=mysqli_query($connection, $query);


    while($row=mysqli_fetch_array($recordset, MYSQLI_ASSOC)) 
    {       
    $UserID=$row['UserID'];
    $LastName = $row['LastName'];
    $FirstName = $row['FirstName'];
    $UserName = $row['UserName'];
    $Phone=$row['Phone'];

    echo "<form action='Update.php' method='get'>";

    echo "<input type='text' name='txtUserID' value='$UserID' disabled> <br/> <br/>";
    echo "<input type='text' name='txtLastName' value='$LastName'> <br/> <br/>";
    echo "<input type='text' name='txtFirstName' value='$FirstName'> <br/> <br/>";
    echo "<input type='text' name='txtUserName' value='$UserName'> <br/> <br/>";
    echo "<input type='text' name='txtPhone' value='$Phone'> <br/> <br/>";

    echo "<input type='submit' name='btnUpdate' value='Update Values' />";

    echo "</form>";     

        }

The problem is here, There are no changes on the database when I execute the query. I don't know the reason. Is the query wrong?? Or there are another way to Update fields from php using an INT field as Parameter in WHERE condition? Thank you in advance.

This is Update.php

$userID=$_GET["txtUserID"];
$lastName=$_GET["txtLastName"];
$firstName=$_GET["txtFirstName"];
$userName=$_GET["txtUserName"];
$phone=$_GET["txtPhone"];

$connection=mysqli_connect($db_host, $db_user, $db_password, $db_name);

$query="UPDATE User SET LastName='$lastName', FirstName='$firstName', UserName='$userName', Phone=$phone
                    WHERE UserID=$userID";

$recordset=mysqli_query($connection, $query);

        echo "<h1> Succesfull Operation.  </h1>";

Try using these:

    $mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);
    $results = $mysqli->query("Select UserID, LastName, FirstName, UserName, Phone from User where UserID=".$searchValue." ") or mysqli0;
  if(!$results){
    echo '<script>alert("not working"); </script>';
  }"

I think that the execution of your mysqli is having an issue, this is how I used mysqli.

You can print the mysql error by changing your last bit of code to

 if (!($recordset=mysqli_query($connection, $query))) {
        echo "<pre>Error:".mysqli_error($link)."</pre>";
    }

and get a better idea of what's wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM