简体   繁体   English

使用PHP表单从MySQL更新字段

[英]Update fields from MySQL using php form

I have been writting a script to update fields in MySQL from a form using php. 我一直在写一个脚本,使用php从MySQL表单更新MySQL字段。

I have an issue, when I try to update the field since php using mysqli, there are no changes on my database. 我有一个问题,当我尝试使用mysqli从php开始更新字段时,我的数据库没有任何更改。 Here is the code, I use 2 pages. 这是代码,我用2页。 Search.php and Update.php . Search.phpUpdate.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? 还是在WHERE条件下使用INT字段作为参数从php更新字段的另一种方法? Thank you in advance. 先感谢您。

This is Update.php 这是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. 我认为mysqli的执行存在问题,这就是我使用mysqli的方式。

You can print the mysql error by changing your last bit of code to 您可以通过将最后一段代码更改为来打印mysql错误

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

and get a better idea of what's wrong. 并更好地了解问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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