Jump to content

update not working


MsKazza
Go to solution Solved by maxxd,

Recommended Posts

Hi all,

Hoping someone might have an insight as to why this isn't working for me.  The post is working as everything echo's ok, and there are no error messages, however it doesn't update at all.  The db connection must be working as the text boxes on forms have an echo of the current content which displays ok.

	$recordID = $_GET["recordID"];
	
	 $result = mysqli_query($con,"SELECT * FROM news WHERE news_id = '$recordID'");
$row = mysqli_fetch_array($result);

	
if (isset($_POST['submit'])) {
$news_id = $_POST['recordID'];
$news_title = $_POST['news_title'];
$news_content = $_POST['news_content'];
$publish = $_POST['publish'];
$facebook = $_POST['facebook'];

echo $news_id;
echo $news_title;
echo $news_content;
echo $publish;
echo $facebook;

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


// Perform queries 
mysqli_query($con,"UPDATE news SET news_title='$news_title', news_content='$news_content', publish='$publish', facebook='$facebook' WHERE news_id='$news_id'");


mysqli_close($con);
		    

Link to comment
Share on other sites

  • Solution

There's no way for us to tell why it's failing right now as the SQL looks fine. What you need to do is check the results of the query. See if the query failed; if it did check the error using mysqli_error().

if(!mysqli_query($con, "UPDATE news SET news_title='$news_title', news_content='$news_content', publish='$publish', facebook='$facebook' WHERE news_id='$news_id'")){
	print("<p>Don't print the error to screen in a real application; but for testing purposes the error is ".mysqli_error($con)."</p>");
}

Also, please note that you're inviting SQL injection by putting suspect data ($_POST values) directly into the SQL. Look into prepared statements - honestly, here's where you're going to find PDO much easier to deal with.

Link to comment
Share on other sites

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.