Page 1 of 1
Still struging to update a table
Posted: Sun Sep 19, 2010 10:49 am
by shehan31
Code: Select all
<?php
echo"<h> Void a Transaction here </h>";
$connect = mysql_connect ("localhost","root","") or die ("error");
mysql_select_db ("guestbook") or die ("eroor connecting Database");
$queryget = mysql_query ("select * from report ORDER BY Transaction_Number DESC LIMIT 5" ) or die (" error with table");
//this while loop will get the information from the data base and it will be displayed on the web interface.
echo "<hr>";
while ( $row = mysql_fetch_assoc ($queryget) ){
$id = $row['id'];
$Customername = $row ['Customer_name'];
$Transaction_Number = $row ['Transaction_Number'];
$Price = $row ['Price'];
$Weight = $row ['Weight'];
$date = $row ['Date'];
$time = $row ['Time'];
echo"
<form action='void.php' method='POST'>
<table width= '35 %' border = '3px'><blockquote></blockquote>
<tr>
<td>
<b> Customer name : $Customername</b>
</td>
</tr>
<tr>
<td>
<b> Weight : $Weight </b>
<p>
<input type='hidden' name='id' value= '$row [id]'/>
<input type='submit' name='submit' value='Void'/>
</td>
</tr>
</table>
</form>
";
}
// if the submit(void)) button is not clicked then the row id (called at the top of the code)will be set to an array.
if (!isset($_POST['submit']))
{
$q = "SELECT Status from report WHERE id = $row[id]";
$result = mysql_query($q);
$row = mysql_fetch_array($result);
}
//if the void button is clicked the status of the perticular record will be updated as cancel using the row id.
if (isset($_POST['submit'])){
$u = "UPDATE report SET Status = 'cancel' WHERE id = $row[id]";
mysql_query($u);
}
---------------------------------------------------------------------------------------------------------------------------------
hi all :
I have been trying to update the table using the above system. the idea is only to change the status as
cancel of the perticular record. I have posted this topic before but could not get a good reply. After that I have tried few other methods and faild. As a result I am posting it once again hoping I may get a better reply from a person who knows this subject better than me. I am reletivley new to this subject.
Thanking all
Shehan31
Re: Still struging to update a table
Posted: Sun Sep 19, 2010 5:36 pm
by Jonah Bron
Replace this part:
Code: Select all
$q = "SELECT Status from report WHERE id = $row[id]";
$result = mysql_query($q);
$row = mysql_fetch_array($result);
With
Code: Select all
$q = "SELECT Status from report WHERE id = $row[id]";
$result = mysql_query($q) or die(mysql_error());
$row = mysql_fetch_array($result);
mysql_fetch_array() expects a query response to be given it, but it says it's getting a boolean value (true/false). mysql_query returns false if it fails, so "or die(mysql_error())" tells us why it's failing.
BTW, I'm not sure but I think the admins would want you to just post the error message instead of several large screenshots.
Re: Still struging to update a table
Posted: Sun Sep 19, 2010 5:42 pm
by yacahuma
Why do you creatre a form for each item. Why not just
Code: Select all
<a href="void.php?id=WHATEVER">Void</a>
Re: Still struging to update a table
Posted: Mon Sep 20, 2010 4:40 am
by shehan31
Jonah Bron wrote:Replace this part:
Code: Select all
$q = "SELECT Status from report WHERE id = $row[id]";
$result = mysql_query($q);
$row = mysql_fetch_array($result);
With
Code: Select all
$q = "SELECT Status from report WHERE id = $row[id]";
$result = mysql_query($q) or die(mysql_error());
$row = mysql_fetch_array($result);
mysql_fetch_array() expects a query response to be given it, but it says it's getting a boolean value (true/false). mysql_query returns false if it fails, so "or die(mysql_error())" tells us why it's failing.
BTW, I'm not sure but I think the admins would want you to just post the error message instead of several large screenshots.
Hi jonah bron;
thank you for the reply.
I have changed the code as you said. but its not working.
<b>Warning: mysql_error() expects parameter 1 to be resource, string given in C:\wamp\www\guestbook\void.php on line 54</b>
Re: Still struging to update a table
Posted: Mon Sep 20, 2010 5:03 am
by shehan31
yacahuma wrote:Why do you creatre a form for each item. Why not just
Code: Select all
<a href="void.php?id=WHATEVER">Void</a>
hi;
thank you for your reply. yes but not for each item. whan i changed the mthod as get (not this code something that I have tried before) it works only once. What happend was when i clicked on any void button the last entry will only be change its status. After that it will not work. This is my latest attempt on it and looks like its not working too. This is where i am struggling for some period.
Thanking you
Re: Still struging to update a table
Posted: Tue Sep 21, 2010 2:11 pm
by Jonah Bron
Try this.
Code: Select all
$q = "SELECT Status from report WHERE id = $row[id]";
$result = mysql_query($q, $connect) or die(mysql_error($connect));
$row = mysql_fetch_array($result);
Re: Still struging to update a table
Posted: Wed Sep 22, 2010 6:00 am
by shehan31
Thank you for your reply jonah. its not working. I am thinking of a different method to fullfill the user requirments. I have been working with this for many weeks and looks like i cannot find the correct solution. Felt like I am missing a very liitle part of this.
Jonah Bron wrote:Try this.
Code: Select all
$q = "SELECT Status from report WHERE id = $row[id]";
$result = mysql_query($q, $connect) or die(mysql_error($connect));
$row = mysql_fetch_array($result);
Re: Still struging to update a table
Posted: Wed Sep 22, 2010 7:29 pm
by Jonah Bron
What do you mean it's not working? Are you saying the "mysql_error() expects parameter one to be resource..." error is still there?
Re: Still struging to update a table
Posted: Wed Sep 22, 2010 9:54 pm
by mikosiko
shehan31 wrote:..... its not working. I am thinking of a different method to fullfill the user requirments. I have been working with this for many weeks and looks like i cannot find the correct solution. Felt like I am missing a very liitle part of this
best if you look for a different method... the code that you showed has several mistakes... it is not going to work at all in the way it is now... to test just do this small changes in your code and you will realize some of the problems that you code has: NOTE: this changes ARE NOT going to fix your problems... only will help to debug and show you some of the problems.
- Insert this lines at the beginning
Code: Select all
<?php
// Here we define how we are going to display errors
error_reporting(E_ALL);
ini_set("display_errors", 1);
- change your the LIMIT in your select from 5 to 2 (this is only to give you enough screen to see the errors message)... cosmetic
- changes this group of lines in this way
Code: Select all
if (!isset($_POST['submit']))
{
echo "What Row Im working with if 1 :" . $row['id'];
$q = "SELECT Status from report WHERE id = $row[id]";
$result = mysql_query($q) or die("Query Fail : " . mysql_error());
$row = mysql_fetch_array($result);
}
//if the void button is clicked the status of the perticular record will be updated as cancel using the row id.
if (isset($_POST['submit']))
{
echo "What Row Im working with if 2 :" . $row['id'];
$u = "UPDATE report SET Status = 'cancel' WHERE id = $row[id]";
mysql_query($u) or die("Update Fail : " . mysql_error());
}
last and no least... those IF's (in reality they should be only one) should be moved to the beginning of the code... in the way they are now are causing multiples refresh/execution of the first connect/select/while loop.... no the best way to do it.
hope the error messages and the echoes help you to visualize your code's problems...
you should look again yacahuma's suggestion... or you can look in these links for 2 possibles approach examples to improve your code (sure there are several others alternatives out there)
http://www.theblog.ca/update-multiple-rows-mysql
http://www.phpeasystep.com/mysql/10.html
Re: Still struging to update a table
Posted: Thu Sep 30, 2010 3:59 am
by shehan31
hi mikosiko;
Thank you for your suggestions. I have changed my mind to look for a different method which will satisfy the user requirments. That will not update the table. The solution is, I have created a different table called refund. if something goes wrong the operator has to refund the transaction first and then have to add a new transaction. Thats my solution for this matter.
mikosiko wrote:shehan31 wrote:..... its not working. I am thinking of a different method to fullfill the user requirments. I have been working with this for many weeks and looks like i cannot find the correct solution. Felt like I am missing a very liitle part of this
best if you look for a different method... the code that you showed has several mistakes... it is not going to work at all in the way it is now... to test just do this small changes in your code and you will realize some of the problems that you code has: NOTE: this changes ARE NOT going to fix your problems... only will help to debug and show you some of the problems.
- Insert this lines at the beginning
Code: Select all
<?php
// Here we define how we are going to display errors
error_reporting(E_ALL);
ini_set("display_errors", 1);
- change your the LIMIT in your select from 5 to 2 (this is only to give you enough screen to see the errors message)... cosmetic
- changes this group of lines in this way
Code: Select all
if (!isset($_POST['submit']))
{
echo "What Row Im working with if 1 :" . $row['id'];
$q = "SELECT Status from report WHERE id = $row[id]";
$result = mysql_query($q) or die("Query Fail : " . mysql_error());
$row = mysql_fetch_array($result);
}
//if the void button is clicked the status of the perticular record will be updated as cancel using the row id.
if (isset($_POST['submit']))
{
echo "What Row Im working with if 2 :" . $row['id'];
$u = "UPDATE report SET Status = 'cancel' WHERE id = $row[id]";
mysql_query($u) or die("Update Fail : " . mysql_error());
}
last and no least... those IF's (in reality they should be only one) should be moved to the beginning of the code... in the way they are now are causing multiples refresh/execution of the first connect/select/while loop.... no the best way to do it.
hope the error messages and the echoes help you to visualize your code's problems...
you should look again yacahuma's suggestion... or you can look in these links for 2 possibles approach examples to improve your code (sure there are several others alternatives out there)
http://www.theblog.ca/update-multiple-rows-mysql
http://www.phpeasystep.com/mysql/10.html