0

I know I am going get sticks from you guys for using mysql_** but I have to at the moment as I am far into the project and I cannot go back and start again. So please be gentle.

Anyway, I don't understand why my update table doesn't work!!

This is the code:

    <?php

$host="localhost"; // Host name
$username="mydetails"; // Mysql username
$password="XXXXXXX"; // Mysql password
$db_name="mydetails"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];

// Delete data in mysql from row that has this id
$result = mysql_query ("UPDATE $tbl_name SET balance='$balance' WHERE id='$id'");


// if successfully deleted
if($result){
echo "The user has been banned successfully!";
echo "<BR>";
echo "<a href='suspend_users.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?>

<?php
// close connection
mysql_close();
?>

If I remove the $balance and leave '' empty or replace $balance with a text or number, it will enter it the balance column which means it does work and connects successfully to the database without any issue. But when I say balance='$balance' it doesn't work at all.

I do have a input field with id="balance" and name="balance" as well so that is not missing.

Can someone please help me out with this without giving me a griff for using mysql_*?

Thanks

Update:

my form code:

<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['username']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['balance']; ?></td>
<td bgcolor="#FFFFFF"><input name="balance" type="text" id="balance" value="<? echo $rows['balance']; ?>" size="15"></td>
<td bgcolor="#FFFFFF"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">Ban and Remove</a></td>
</tr>
andr
  • 15,970
  • 10
  • 45
  • 59
user2056633
  • 151
  • 1
  • 16

4 Answers4

0

Try this

$result = mysql_query ("UPDATE ". $tbl_name ." SET balance='".$balance."' WHERE id='".$id."'");
MiniGod
  • 3,683
  • 1
  • 26
  • 27
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0

The values of form cannot be accessed using variable names they are stored in global arrays $_GET or $_POST (depends which method you use in form). You may try this:

// get value of id that sent from address bar
$id=$_GET['id'];

//get value of balance. i am supposing you have used get method in your form
$balance = $_GET['balance'];

// Delete data in mysql from row that has this id
$result = mysql_query ("UPDATE $tbl_name SET balance='$balance' WHERE id='$id'");
Code Prank
  • 4,209
  • 5
  • 31
  • 47
-1

just put $tbl_name in quotes like this '$tbl_name'

Raul
  • 579
  • 1
  • 5
  • 17
  • ideally it should work. don't know who put my answer in negative!! i have executed more than 1K queries like this. Do one thing try to echo your update statement. i guess balance variable must be having some problem – Raul Feb 12 '13 at 05:44
  • -1 This is not his issue, and does not fix his problem. It's valid, but not a fix. – MiniGod Feb 12 '13 at 05:49
  • @ScoRpion... Is this one of his issues? `UPDATE TABLE members SET...` and `UPDATE TABLE 'members' SET...` will do exactly the same in this case. – MiniGod Feb 12 '13 at 05:55
  • @MiniGod but $result = mysql_query ("UPDATE $tbl_name SET balance='$balance' WHERE id='$id'");. and when you will print this it will display like this UPDATE members SET balance='$balance' WHERE id='$id' rather values of $balance and Id – ScoRpion Feb 12 '13 at 06:08
  • that is happening because of mysql_query remove this and then try to print – Raul Feb 12 '13 at 06:11
  • @ScoRpion... I believe you're the one being confused about double and single quotes... – MiniGod Feb 12 '13 at 06:13
  • @MiniGod, the thing is it hasn't. because I've tried all the methods mentioned above and still doesn't work. I've tried your $balance = $_POST['balance'] method as well and didnt work. – user2056633 Feb 12 '13 at 06:44
-1

$balance is never set in your code.

Try adding $balance = $_POST['balance'],

Or if the form has method="get" use $balance = $_GET['balance'].

MiniGod
  • 3,683
  • 1
  • 26
  • 27
  • MiniGod, i'm not trying to see whats in the balance column though!! i just need to update the blanace column. – user2056633 Feb 12 '13 at 05:40
  • `$_POST` and `$_GET` are from the form, not the database. – MiniGod Feb 12 '13 at 05:41
  • yes, i understand. but i think you misundrestood what i am trying to do. I have a form as you can see with an input named balance. i need to UPDATE the balance column in the database with the number/value that I enter in the balance input field. – user2056633 Feb 12 '13 at 05:45
  • Yes, exactly. So, you take the value of the `$_POST['balance']`, which comes from the the input in the form, and send it to the database using `UPDATE TABLE`. Try it! – MiniGod Feb 12 '13 at 05:47