1

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>
11
  • Do not use string concatenation in SQL queries. Use prepared statements. Commented Sep 17, 2015 at 11:49
  • Change your die("You couldn't...") function to die(mysqli_error($connection)), so that you will see the error description. Commented Sep 17, 2015 at 11:50
  • put $id into quote '$id' Commented Sep 17, 2015 at 11:51
  • $CUST_FORENAME='$name' ? Shouldn't you delete that first character? :), as in CUST_FORENAME='$name' Commented Sep 17, 2015 at 11:51
  • did you include the page where the connection is Commented Sep 17, 2015 at 11:51

2 Answers 2

3

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'";
Sign up to request clarification or add additional context in comments.

Comments

1

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.";

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.