Jump to content

Recommended Posts

Simple statement, but it's not functioning...  Just a simple update query, only I get NO error, and NO update... Here's the code...

 

mysql_query("UPDATE contacts SET fname = '$_POST[formName]' WHERE ID = '$rec'");

 

$rec used to be $_POST[formid] which is the ID of the entry in the database... The variables passed have been verified as accurate, but still no error, and no update... My eyes hurt from staring at this for this long, any sets of eyes see something I don't?

 

(p.s., I'm not a newbie in this by any means, mostly why this is confusing me so much... )

 

Thanks in advance for any help!

 

Osyrys

Link to comment
https://forums.phpfreaks.com/topic/241940-mysql-simple-statement-malfunction/
Share on other sites

I assume that you have prooperly debugged your query before posting..? try this

 

$form_name = $_POST['formName'];
if(!empty($_POST['formName'])){
      $form_name = mysql_real_escape_string($form_name);
      $query = mysql_query("UPDATE contacts SET fname = '$form_name' WHERE ID = $rec");
      if(!$query){
            exit(mysql_error());
      }else{
            print "query was a success";
      }
}

 

If this does not work for you, I will need to see more of your code

p.s., I'm not a newbie in this by any means, mostly why this is confusing me so much...

 

Things can be overlooked by even the best of programmers.  I would suggest standard de-bugging protocol.

error_reporting(E_ALL);
ini_set('display_errors','On');
mysql_query("UPDATE contacts SET fname='" . mysql_real_escape_string($_POST['formName']) . "' WHERE ID = '" . intval($rec) . "'") or trigger_error( mysql_error() );

The only thing that shows up different is this message....

 

Notice: Use of undefined constant formid - assumed 'formid' in /home/acinquemani/public_html/forms/updatecontact.php on line 24

 

And line 24 is just echoing the variable to make sure that is flowing to the page.... :(  No other messages or errors.

 

Osyrys

Whenever a query does not do as you expect - echo it to the page to validate it contains what you think it does. I would advise NEVER actually building your query inside the mysql_query() function. Instead build it as a string variable so you can see what it contains. You say the variables have been verified, but since you don't show the code of how you verified them I can't take your word for it. Also, although an array variable such as "$_POST[formName]" will work; it is wrong. That is not my opinion, that is stated in the PHP manual several times. Always enclose string keys in quotes.

 

Building upon jcbones's code and adding more error handling and debugging code. This will show if there are any errors and what the results of the query are. If this still doesn't show any affected rows, then there is no record matching the ID given. Make sure you are running against the right database. it wouldn't be the first time someone updated a page in one environment and was testing against a different one.

error_reporting(E_ALL);
ini_set('display_errors','On');

//Escape/prepare values for query
$fnameValue = mysql_real_escape_string(trim($_POST['formName']));
$id =intval($rec);
//Create query
$query = "UPDATE contacts SET fname='{$fnameValue}' WHERE ID={$id}";

//Debugging line: echo query
echo "Query : $query<br>\n";;

//Run query
mysql_query($query) or die(mysql_error());

//Debugging line: echo line affected
echo "Records updated: " . mysql_affected_rows();

 

you did not wrap the index in quotes, so the parser assumes this as a constant, which it is not, try my code

Assuming there is an array index of "formid" that would not be the problem. It is wrong - but it will work, which is why it is a "Notice" and not an error.

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.