简体   繁体   中英

Update sql query with PHP and variables

When I run this page, its says You couldnt execute query . I think there is a syntax problem, but I cannot see the problem.

The variables look fine when I echo them. I added the column names as a code comment. All the columns are VARCHAR .

I don't believe it is a connection problem since I can perform SELECT AND DELETE operations from other php pages.

<?php 

$id = $_POST["id"];
$name = $_POST["name"];
$surname = $_POST["surname"];
$phone = $_POST["phone"];

/* Table in the DDBB(all are VARCHAR): CUST_ID  CUST_FORENAME   CUST_PHONE  CUST_SURNAME*/

$query = "UPDATE customers SET CUST_ID='$id',$CUST_FORENAME='$name',
            CUST_PHONE='$phone', CUST_SURNAME='$surname' WHERE CUST_ID=$id";

$result = mysqli_query ($connection,$query)
    or die ("You couldn’t execute query");

echo "<br /><br />User $id has been updated.";

?>
</div>
</body>
</html>

Change your query as follows and it will work

$query = "UPDATE customers SET CUST_ID='$id', CUST_FORENAME='$name',
                 CUST_PHONE='$phone', CUST_SURNAME='$surname' WHERE CUST_ID='$id'";

Thanks guys, It was just a $ sign in one of the column and put back again quotes in the $id > '$id'.

$id = $_POST["id"];
$name = $_POST["name"];
$surname = $_POST["surname"];
$phone = $_POST["phone"];

/*CUST_ID   CUST_FORENAME   CUST_PHONE  CUST_SURNAME*/

$query = "UPDATE customers SET CUST_ID='$id',CUST_FORENAME='$name',
            CUST_PHONE='$phone', CUST_SURNAME='$surname' WHERE CUST_ID='$id'";

$result = mysqli_query ($connection,$query)
    or die(mysqli_error($connection));

echo "<br /><br />User $id has been updated.";

?>

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