Jump to content

Archived

This topic is now archived and is closed to further replies.

CMellor

MySQL UPDATE code not updating

Recommended Posts

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)

[code]<?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; ?>[/code]

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

[img]http://img312.imageshack.us/img312/4999/pictf1.jpg[/img]

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.

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
So I am assuming that you mean it should be like this:

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

---

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.