Jump to content


Photo

MySQL UPDATE code not updating


  • Please log in to reply
4 replies to this topic

#1 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 02 August 2006 - 09:54 PM

Hello, I have spent the last few hours building and trying to make it so this code updates my result in the database. For some reason it will not Update and I really cannot figure it out ??? and I have come here to gain some expert help, and really hope you can help me out with my problem.

This is my PHP code (it's not long)

<?php
/* USER POINTS */
if($do == "interactive_rankings_user_points"):
?>

<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td class="header">Search for Member</td>
  </tr>
  <tr>
    <td class="row1">
	<table width="100%" border="1" cellspacing="0" cellpadding="0">
	  <tr>
		<td class="row2">
		<form action="" method="post">
		<input name="name" type="text" style="width: 75%" /> <input name="search" type="submit" value="Search" />
		</form>
		</td>
	  </tr>
	</table>
	</td>
  </tr>
</table>
<br />

<?php
if($_POST['search']) {

if($_POST['change']) {
  mysql_query("UPDATE `ibf_members` SET `points` = '".$_POST['amount']."' WHERE `members_display_name` = ".$_POST['mdn']."");
}

if(empty($_POST['name'])) {
  echo('<div class="error">The text field was left empty</div>');
} else {

$query = mysql_query("SELECT * FROM ibf_members WHERE members_display_name LIKE '%".$_POST['name']."%'");
$count = mysql_num_rows($query);
?>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td class="header">Search Results - <?=$count?> results found</td>
  </tr>
  <tr>
    <td class="row1">
	<table width="100%" border="1" cellspacing="0" cellpadding="0">
<?php while($row = mysql_fetch_array($query)) { ?>
  <form action="" method="post">
  <tr>
    <td class="row2" width="75%"><?=$row['members_display_name']?></td>
	<td class="row2" width="25%">
	<input type="hidden" name="mdn" value="<?=$row['members_display_name']?>" />
	<input type="button" value="+" onclick="javascript:this.form.amount.value++;" />
	<input name="amount" type="text" style="text-align: center" value="<?=$row['points']?>" size="4" maxlength="4" />
	<input type="button" value="-" onclick="javascript:this.form.amount.value--;" />
	<input name="change" type="submit" value="Change" />
	</td>
  </tr>
  </form>
<?php } ?>
    </table>
	</td>
  </tr>
</table>
<?php } } /* END USER POINTS */ endif; ?>

I also have a picture of what it produces seeing as my localhost doesn't allow access outside of my computer

Posted Image

The top box is a search form and the bottom form is two results after a search. Next to each result is a input with the amount of points the user has and that is grabbed via the database. On either side of it is a button to increase and decrease the value and a submit button to be able to change it to the value you put it as. When clicked Change, the search results disapeer and it doesn't update the database, so from the code provided, can anybody tell me why?

Thank you very much, I look forward to hearing from your replies.

Chris.

#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 02 August 2006 - 10:20 PM

Several points
Fieldnames and Table names dont need single quotes (')
mysql_query("UPDATE ibf_members SET points = '".$_POST['amount']."' WHERE members_display_name = ".$_POST['mdn']."");

and do you know how risky it is having code going straight into the query, especially update

I recommend using this

$amount = mysql_real_escpae_string($_POST["amount"];
$mdn = mysql_real_escape_string($_POST["mdn"];

$query = "UPDATE ibf_members SET points = '".$amount."' WHERE members_display_name = ".$mdn."'";
$result = mysql_query($query);

if($result){
//do Whatever
}else{
echo "There has been an error.<br />".mysql_error();
}
Tell me the problem, I will try tell you the solution

#3 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 02 August 2006 - 10:31 PM

onlyican, those are not single quotes. they are backticks and although not required around table and column names, they're there to help in case a column name is parsed incorrectly as a keyword.

to CMellor, you're missing the single quotes for the value of members_display_name in the update query. if you've debugged with mysql_error, you should probably have seen it.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#4 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 02 August 2006 - 11:53 PM

So I am assuming that you mean it should be like this:

if($_POST['change']) {
  mysql_query("UPDATE `ibf_members` SET `points` = '".$_POST['amount']."' WHERE `members_display_name` = '".$_POST['mdn']."'");
}

---

I tried this and it still doesn't update, the search results still disappear and the table does not update to the changed value :( I can't think where I'm going wrong, so I still hope I can get some more help.

Thanks for the advice so far.

#5 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 12:25 AM

trust me on this for a minute, (just make me happy and read it at least

change the code to

$query = "UPDATE `ibf_members` SET `points` = '".$_POST["amount"]."' WHERE `members_display_name` = '".$_POST["mdn"]."'";

$result = mysql_query($query);

if($result){
echo "Should have worked";
}else{
echo "There has been an error.<br />".mysql_error();
}

Just try that and see what happens

The reason we do that is we can also do
echo $query;
to find out if the 2 vars have values

ryanlwh: I used the back tick thingies, first for everything i suppose

Tell me the problem, I will try tell you the solution




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users