Page 1 of 1

writing into the database and a php problem...

Posted: Mon Aug 29, 2005 1:49 pm
by The-Master
Hi, i need to know how can i write into a field in a table in my database, i forgot the full function... i only remember UPDATE... :oops: ... so if anyone knows the function it will be realy helpful... and the problem i have in php is that i need to make a form where users can withdraw/deposit money so i made script:

Code: Select all

<?php
echo "$_SESSION[username] , &#1489;&#1512;&#1493;&#1498; &#1492;&#1489;&#1488; &#1500;&#1489;&#1504;&#1511;. &#1502;&#1492; &#1514;&#1512;&#1510;&#1492; &#1500;&#1506;&#1513;&#1493;&#1514;?";
$result=mysql_query("SELECT * FROM RPG2");
$storage=mysql_result($result,$id,"storage");
$storage_limit=mysql_result($result,$id,"storage_limit");
echo "<table><tr><td>";
echo "<font color='white'>&#1499;&#1505;&#1507; &#1502;&#1488;&#1493;&#1495;&#1505;&#1503;: $storage <img src='cerion.gif'></font><br>";
echo "</td><td><img src='icons/chest.png'></td></tr></table>";
echo "<font color='#AAAABB'><b>&#1502;&#1490;&#1489;&#1500;&#1514; &#1488;&#1495;&#1505;&#1493;&#1503;: &#1506;&#1491; $storage_limit </b><img src='cerion.gif'></font><br>";
echo "<table><tr><td>";
echo "<form method='post' action='bank.php'>";
echo "<font color='white'>&#1502;&#1513;&#1493;&#1498; &#1499;&#1505;&#1507;: <input type='text' name='withdraw' value='' size='11'></font>
<br><input type='submit' name='withdraw' value='deposit' size='6'>";
echo "</td><td><img src='icons/withdraw.png'></td></tr></table>";
$sql4 = "SELECT * FROM RPG WHERE id = '$id'";
$result4 = mysql_query($sql4);
$r=mysql_fetch_array($result4);
$money=$r["money"];
echo "<table><tr><td>";
$result2=mysql_query("SELECT * FROM RPG");
// $money=mysql_result($result2,$id,"money");
$rm=mysql_fetch_array($result2); 
$money=$rm['money'];
echo "<font color='white'>&#1492;&#1508;&#1511;&#1491; &#1499;&#1505;&#1507;: <input type='text' name='deposit' value='' size='11'></font>
<br><input type='submit' name='withdraw' value='withdraw' size='6'></form>";
echo "</td><td><img src='icons/deposit.png'></td></tr></table>";
--------------------------------------- !!! PROBLEM HERE !!! ---------------------------------------
if($_POST['withdraw'] > $storage) {
echo "you can not withdraw this amount of money becuase you do not have it in your storage...";
}
elseif($_POST['withdraw'] <= $storage) {
$withdraw=$_POST['withdraw'];
echo "you withdrawed $withdraw <img src='cerion.gif'>";
}
else {
echo "<div dir='rtl'>";
echo "<li>&#1492;&#1499;&#1505;&#1507; &#1504;&#1511;&#1512;&#1488; &#1505;&#1512;&#1497;&#1493;&#1503;, &#1492;&#1505;&#1497;&#1502;&#1503; &#1513;&#1502;&#1497;&#1497;&#1510;&#1490; &#1488;&#1514; &#1492;&#1499;&#1505;&#1507; &#1492;&#1493;&#1488;: <img src='cerion.gif'>!";
echo "<li>&#1488;&#1514;&#1492; &#1500;&#1488; &#1497;&#1499;&#1493;&#1500; &#1500;&#1502;&#1513;&#1493;&#1498; &#1497;&#1493;&#1514;&#1512; &#1499;&#1505;&#1507; &#1502;&#1502;&#1492; &#1513;&#1497;&#1513; &#1500;&#1498; &#1489;&#1488;&#1495;&#1505;&#1493;&#1503; &#1492;&#1499;&#1505;&#1507;!";
echo "<li>&#1488;&#1514;&#1492; &#1500;&#1488; &#1497;&#1499;&#1493;&#1500; &#1500;&#1492;&#1508;&#1511;&#1497;&#1491; &#1499;&#1505;&#1507; &#1497;&#1493;&#1514;&#1512; &#1502;&#1502;&#1490;&#1489;&#1500;&#1514; &#1492;&#1488;&#1495;&#1505;&#1493;&#1503;!";
echo "</div>";
}
?>
the $storage is a var taken from the database this var includes the amount of money the user has in his storage in the bank. the main problem is that the code doesn't detect the input of the user... if he wrote 100000000 in the withdraw input and he only has 999 money in the storage the script doesn't say he can't withdraw more then he has... :x

Posted: Mon Aug 29, 2005 4:56 pm
by feyd
on submission, check that the ending balance is at or above zero, pretty simple... You can add that to the front end as well, but you can't rely on it happening.

Posted: Tue Aug 30, 2005 5:39 am
by The-Master
i solved the problem my own way i changed the code to this:

Code: Select all

<?php
$user=$_SESSION['username'];
$idq = "SELECT id, user FROM users WHERE user = '$user'";
$idr = mysql_query($idq); 
$e=mysql_fetch_array($idr); 
$id=$e["id"]; 
$id--;
echo "$_SESSION[username] , welcome to the bank. what would you like to do?";
$result=mysql_query("SELECT * FROM RPG2");
$storage=mysql_result($result,$id,"storage");
$storage_limit=mysql_result($result,$id,"storage_limit");
$result2=mysql_query("SELECT * FROM RPG");
$rm=mysql_fetch_array($result2); 
$money=$rm['money'];
echo "<table><tr><td>";
echo "<font color='white'>money in storage: $storage <img src='cerion.gif'></font><br>
unstored money: $money <img src='cerion.gif'></font><br>";
$allce=$storage+$money;
echo "all the money you have: $allce <img src='cerion.gif'></font><br>";
echo "</td><td><img src='icons/chest.png'></td></tr></table><br>";
echo "<font color='#AAAABB'><b>storage limit: you are allowed to store only $storage_limit </b><img src='cerion.gif'> in the storage</font><br><br>";
echo "<table><tr><td>";
echo "<form method='post' action='bank.php'>";
echo "<font color='white'>withdraw money: <input type='text' name='withdraw' value='' size='11'></font>
<br><input type='submit' name='withdraws' value='withdraw' size='6'>";
echo "</td><td><img src='icons/withdraw.png'></td></tr></table>";
echo "<table><tr><td>";
$result2=mysql_query("SELECT * FROM RPG");
$rm=mysql_fetch_array($result2); 
$money=$rm['money'];
echo "<font color='white'>deposit money: <input type='text' name='deposit' value='' size='11'></font>
<br><input type='submit' name='deposits' value='deposit' size='6'></form>";
echo "</td><td><img src='icons/deposit.png'></td></tr></table><br>";
echo "<font color='red'><b>";
if($_POST[withdraw] > $storage) {
echo "you can not withdraw more money then what you have in the storage...";
}
elseif($_POST[withdraw] != '') {
$user=$_SESSION['username'];
$idq = "SELECT id, user FROM users WHERE user = '$user'";
$idr = mysql_query($idq); 
$e=mysql_fetch_array($idr); 
$id=$e["id"]; 
$id--;
$wd=$_POST[withdraw];
echo "you withdrawen <img src='cerion.gif'>$wd,&nbsp";
$bank=$storage-$wd; <---- the result
// MySQL
// MySQL
echo "you now have: <img src='cerion.gif'>$bank in your storage";
}
echo "<b></font>";
?>
but i still have one problem... i need to make that the result of the user's action (withdraw/deposit) will be stored in the database, the table is RPG2 and the field is Storage so i need a code that will update the Storage...
i wrote this code, but it didn't work...:

Code: Select all

$sql = "UPDATE RPG2 SET storage = '$bank' WHERE id = '$id'";
$result = mysql_query($sql);
***the var $bank has a value in the first code ("<---- the result") and where i wrote //MySQL twice there should be the code...