Jump to content

MySQL Error


bravo14

Recommended Posts

Hi Guys

 

I am trying to update an entry in a database using the folliwng code

 

<code>

<?php

  include_once('includes/connect.php');

  foreach ($_POST as $key => $val) {

  $key = mysql_real_escape_string($val);

}

  $res = mysql_query("UPDATE `fixtures` SET `yardley_goals` = '".$yardley_goals."',`opposition_goals` ='".$opposition_goals."', `motm` = '".$motm."'`totw`='".$totw."'`totw_reason`='".$totw_reason."' WHERE `match_id` ='".$match."'");

  echo($res);

{

  die('Error: ' . mysql_error());

  }

echo ("1 record added");

mysql_close($con);

      ?>

</code>

 

I am getting the following error

 

Error: 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 '`totw`=''`totw_reason`='' WHERE `match_id` =''' at line 1

Link to comment
Share on other sites

try

 

$res = mysql_query("UPDATE fixtures SET yardley_goals = '$yardley_goals',opposition_goals ='$opposition_goals', motm = '$motm', totw='$totw', totw_reason='$totw_reason' WHERE match_id ='$match'");

 

Also, you probably have register_globals disabled, so you need to do:

$yardley_goals = mysql_real_escape_string($_POST['yardley_goals']);

 

etc... for each of your variables.

 

Link to comment
Share on other sites

I have updated to the following code and the record is not updating even though it is saying the record has been updated

 

  <?php
  include_once('includes/connect.php');
  foreach ($_POST as $key => $val) {
   $key = mysql_real_escape_string($val);
}
  $res = mysql_query("UPDATE fixtures SET yardley_goals = '$yardley_goals',opposition_goals ='$opposition_goals', motm = '$motm', totw= '$totw', totwreason = '$totw_reason' where match_id = '$match'");
    if (!$res) 
{
  die('Error: ' . mysql_error());
  }
  else
echo ("1 record added");

$checkresult=mysql_query("SELECT *FROM fixtures where match_id='$match'");

echo('<table class="profiletext">
 <tr><td>Date</td><td>'.$checkresult[match_date].'</td></tr>
	 <tr><td>Opponent</td><td>'.$checkresult[Opposition].'</td></tr>
 <tr><td>Yardley Goals</td><td>'.$checkresult[yardley_goals].'</td></tr>
 <tr><td>Opposition Goals</td><td>'.$checkresult[opposition_goals].'</td></tr>
 </table>');
mysql_close($con); 
       ?>

 

I have also put in an additional query to find the updated record and that is not displaying the record.

Link to comment
Share on other sites

if (!$res)

The above conditional test only checks if the query was executed without error. It does not test if any row(s) were actually updated. You need to use mysql_affected_rows to find out if the UPDATE query actually changed a row.

 

However, if the row is not being updated, that would imply that the WHERE condition did not match any rows in your table. Have you checked what $match actually contains so that you know the query should find a row in your table?

 

mysql_query(), for your SELECT query, returns a result resource (if the query executes without any errors.) You need to use one of the mysql_fetch_xxxx() functions to actually get a row from the result set. If you were developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON, you would have received error messages when you referenced $checkresult[match_date], $checkresult[Opposition], ... that would have alerted you to the fact that they don't exist as part of the $checkresult variable.

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.