3

Im trying to edit the lastname (lname) but its not working ,

im getting this error :

ERROR: Could not able to execute UPDATE tablename SET fname = '', lname = '' WHERE fname = . You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

<?php
$link = mysqli_connect("IP","DB","PASS (hiden ofc)", "DBN");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt update query execution
$sql = "UPDATE tablename SET fname = '$nfname', lname = '$nlname' WHERE fname = $fname";
if(mysqli_query($link, $sql)){
    echo "Records were updated successfully.";
} else {
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

--- HTML CODE ---

<html>
<body>
<h1>Test editing </h1>
<form action="edit.php" method="post">
OrginalFirstname: <input type="text" name="fname" /><br><br>
Lastname: <input type="text" name="lname" /><br><br>
NewFirstname: <input type="text" name="nfname" /><br><br>
 
<input type="submit" />
</form>
</body>
</html>

4
  • Why are you updating all the fields if you only want to edit the last name? Where are those variables assigned? Commented Dec 25, 2015 at 22:47
  • This is just done for testing purpose , im kinda new to it Commented Dec 25, 2015 at 23:47
  • Is that your entire code? Commented Dec 26, 2015 at 0:17
  • Only update fields that need to be updated. You also should use parameterized queries. Commented Dec 26, 2015 at 0:23

1 Answer 1

4

You forgot to put apostrophes on the last part of your query:

$sql = "UPDATE tablename SET fname = '$nfname', lname = '$nlname' WHERE fname = '$fname'";

This should work.

Make sure you escaped all the variables with mysqli_real_escape_string. If one of the variables has a non-escaped apostrophe, the query will fail again.

If the PHP code in your question is the entire code, then you are not getting the values from the $_POST[].

You can get the values into your variables using extract($_POST); on the beggining of your code.

Sign up to request clarification or add additional context in comments.

2 Comments

I think that's the issue he isn't assigning the variables which he is using.
Aaaah i didnt know i had to add it all seems to be working currently Thank you very much! and sorry for my mistakes

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.