Jump to content

[SOLVED] Could not update page: You have an error in your SQL syntax;


Recommended Posts

I get this error on my page at run time: 

 

Could not update page: 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 'WHERE name = 'body'' at line 3

 

The page is designed to pull data from MySQL database and display it in a text field in which the user is able to edit the text. Then should then be able to press update and pass the new data back to the database.

 

Code is below, I cant work it out because I'm certain that the query is correct.

<?php
$mysqli = mysqli_connect("localhost", "root", "pass", "opp_group");
  
echo" <form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">
      <textarea name=\"body\" cols=\"50\" rows=\"10\">";

if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
else
{	
$getTextHome_sql = "SELECT * FROM home_content WHERE name= 'body'";
$getTextHome_res = mysqli_query($mysqli, $getTextHome_sql) or die (mysqli_error($mysqli));

if ($getTextHome_res) 
{
	while ($newArray = mysqli_fetch_array($getTextHome_res, MYSQLI_ASSOC)) 
	{
		$body = $newArray["content"];
		echo $body;
		$body = $_POST["content"];
	}
} 
else 
{
		printf("Could not retreive records: %s\n", mysqli_error($mysqli));
}

mysqli_free_result($getTextHome_res);
mysqli_close($mysqli);
}	?>

  <?php 
  
  $mysqli = mysqli_connect("localhost", "root", "pass", "opp_group");

  echo "</textarea>
              </td>
    <td align=\"left\" colspan=\"3\"><img src=\"../../Images/homebodychange.jpg\" width=\"350\" height=\"250\" /></td>
  </tr>
  <tr><td><input type=\"submit\" name=\"update\" value=\"Update\" />";
if (mysqli_connect_errno())
{
	printf("Connect failed: %s\n", mysqli_connect_error());
	exit();
}
else
{
	$updateTextHome_sql = "INSERT INTO home_content (content)
							VALUES ('".$_POST["content"]."')
							WHERE name = 'body'";

	$updateTextHome_res = mysqli_query($mysqli, $updateTextHome_sql);

	if ($updateTextHome_res === TRUE)
	{
		echo "Page Updated.";
	}
	else
	{
		printf("Could not update page: %s\n", mysqli_error($mysqli));
	}
	mysqli_close($mysqli);
}
  
  echo"</td></tr>
</table></form>";
?>

 

 

I think it could be caused because I have set the value of $body inside a loop, and perhaps it is not visible outside of this loop. How do I make it visible to the whole script even though it's inside the loop?

$updateTextHome_sql = "UPDATE home_content
				SET content='".$body."'
				WHERE name = 'body'";

 

The $body variable is what populates the field in the first place, so when it gets changed do I need to use a different variable?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.