...

View Full Version : passing values directly to a new page and using them to update values



ciaracous
08-30-2006, 01:25 PM
I'm trying to pass values through from one page to the next by clicking on an image. When I click an image it passes through the values for prodId and shopName


<td align="center"><a href="admin_update2.php?login=true&shopName='.$row['shopName'].'&prodId='.$row['prodId'].'"><img src="http://snet.wit.ie/~ciaracousins/clothes/' . $row['image'] . '"></a>




This is passed through to my admin_update.php where I want to populate a table with all of the details stored on the product that was clicked on. The values are being passed through ok but the table is not being displayed. I have another update page but with this page i am using a submit button which is passing through the details. I tried using this method again but because there is no submit button used (I am just clicking on a link/image) it is not working for me. Below is the code I am trying to use


<?php

include("db.php");

$prodId = $_GET['prodId'];

$sql = "select * from product where prodId = '$prodId'";

//db
//$result = mysql_query($sql,$conn) or die(mysql_error());


if (isset($_POST['submittedUpdate']))
{
$prodId = mysql_real_escape_string(trim($_POST['prodId']));
$sName = mysql_real_escape_string(trim($_POST['shopName']));
$dept = mysql_real_escape_string(trim($_POST['dept']));
$brand = mysql_real_escape_string(trim($_POST['brand']));
$type = mysql_real_escape_string(trim($_POST['type']));
$image = mysql_real_escape_string(trim($_POST['image']));
$price = mysql_real_escape_string(trim($_POST['price']));

$query2 = "UPDATE product SET prodName='$prodName', dept='$dept', brand='$brand', type='$type', image='$image', price='$price' WHERE prodId ='$prodId' and shopName='$shopName'";

$query = "SELECT * FROM product WHERE prodId = '$prodId'";


if ($result = mysql_query($query))
{

$row = mysql_fetch_array($result, MYSQL_ASSOC)
?>
<form action="admin_update2.php" method="post">
<input type="hidden" name="prodId" value="<?php echo $prodId; ?>">

<center>
<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">

<TR><TD align="left" bgcolor="#2696b8" width="30%"><FONT color="white"><B>Product Id: </B></FONT></TD>
<TD width="70%"><?php echo $row['prodId'] ?></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Shop Name:</B></FONT></TD>
<TD width="70%"><?php echo $row['shopName'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Product Name:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name=name value="<?php echo $row['prodName'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Department:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name="surname" value="<?php echo $row['dept'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Brand:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name="address" value="<?php echo $row['brand'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Type:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name="address2" value="<?php echo $row['type'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Image:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name="county" value="<?php echo $row['image'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Price:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name="county" value="<?php echo $row['price'] ?>"></TD></TR>

<TR><TD></td>
<TD><CENTER><input name=submit type=submit value="UPDATE">
<FONT color=navy></FONT></P></TD></TR>
</TBODY>


<input type="hidden" name="prodId" value='.$prodId.'>
<input type="hidden" name="submittedUpdate" value="TRUE"/></td></tr>

</TABLE>
</FORM>
<?php
}
}

?>

</body>
</html>




would be really grateful if somebody could point out what modifications need to be made

Fumigator
08-30-2006, 03:17 PM
At first glance it looks like you are using the $_POST array when you should be using the $_GET array. The $_GET array contains all the variables from the url you used (&shopName=&prodId= etc). The $_POST array contains form elements from a form that gets submitted.


$prodId = mysql_real_escape_string(trim($_POST['prodId'])); //wrong
$prodId = mysql_real_escape_string(trim($_GET['prodId'])); //right

ciaracous
08-30-2006, 03:54 PM
I have no gotten it to display the values into the table by using



$result = mysql_query($sql,$conn) or die(mysql_error());

if (mysql_num_rows($result) == 1) {

//if authorized, get the values of prodId, shopName...

$prodId = mysql_result($result, 0, 'prodId');

But now it is not actually updating anything for me because in my method for updating I was using the submittedupdate which I am no longer using.



<?php

include("db.php");
$prodId = 0; // UPDATED
$prodId = $_GET['prodId'];

$sql = "select * from product where prodId = '$prodId'";

//db
$result = mysql_query($sql,$conn) or die(mysql_error());

if (mysql_num_rows($result) == 1) {

//if authorized, get the values of prodId, shopName...

$prodId = mysql_result($result, 0, 'prodId');
$shopName= mysql_result($result, 0, 'shopName');
$dept = mysql_result($result, 0, 'dept');
$brand = mysql_result($result, 0, 'brand');
$type = mysql_result($result, 0, 'type');
$image = mysql_result($result, 0, 'image');
$price = mysql_result($result, 0, 'price');



$query2 = "UPDATE product SET prodName='$prodName', dept='$dept', brand='$brand', type='$type', image='$image', price='$price' WHERE prodId ='$prodId' and shopName='$shopName'";

$query = "SELECT * FROM product WHERE prodId = '$prodId'";


if ($result = mysql_query($query))
{

$row = mysql_fetch_array($result, MYSQL_ASSOC)
?>
<form action="admin_update2.php" method="post">
<input type="hidden" name="prodId" value="<?php echo $prodId; ?>">

<center>
<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">

<TR><TD align="right" bgcolor="#2696b8" width="30%"><FONT color="white"><B>Product Id: </B></FONT></TD>
<TD width="70%"><?php echo $row['prodId'] ?></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Shop Name:</B></FONT></TD>
<TD width="70%"><?php echo $row['shopName'] ?> </TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Product Name:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name=name value="<?php echo $row['prodName'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Department:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name="surname" value="<?php echo $row['dept'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Brand:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name="address" value="<?php echo $row['brand'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Type:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name="address2" value="<?php echo $row['type'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Image:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name="county" value="<?php echo $row['image'] ?>"></TD></TR>

<TR><TD align="right" bgcolor="#2696b8"><FONT color="white"><B>Price:</B></FONT></TD>
<TD><INPUT type="text" maxLength=45 size=45 name="county" value="<?php echo $row['price'] ?>"></TD></TR>

<TR><TD></td>
<TD><CENTER><input name=submit type=submit value="UPDATE">
<FONT color=navy></FONT></P></TD></TR>
</TBODY>


<input type="hidden" name="prodId" value='.$prodId.'>
<input type="hidden" name="submittedUpdate" value="TRUE"/></td></tr>

</TABLE>
</FORM>
<?php
}
}

?>

</body>
</html>


Could you point out what changes are needed to suit this new code please?

Fumigator
08-30-2006, 05:14 PM
You never actually run the update query, you just assign the variable $query2 to a query statement string. You must call mysql_query($query2). Make sure to check it for success or failure!

By the way, this code here is really unnecessary:


$prodId = mysql_result($result, 0, 'prodId');
$shopName= mysql_result($result, 0, 'shopName');
$dept = mysql_result($result, 0, 'dept');
$brand = mysql_result($result, 0, 'brand');
$type = mysql_result($result, 0, 'type');
$image = mysql_result($result, 0, 'image');
$price = mysql_result($result, 0, 'price');


Instead of using mysql_result(), you can use mysql_fetch_assoc() which creates an associative array of your results. The index names are made up of the field names in your table. Very handy.



$productInfo = mysql_fetch_assoc($result);
echo "For example, brand is " . $productInfo['brand'];


I just noticed the logic of your program is
1. Select data where the ID = $_GET value
2. Update where id = $_GET value
3. Select data where the ID = $_GET value

I'm not seeing the point of the update and the second select, since it's going to update using the data it already has.

ciaracous
08-30-2006, 05:43 PM
I'm not a coder and am really new to this so I dont really graspe the logic behind it to be honest. What i did was put this




$query = "SELECT * FROM product WHERE prodId = '$prodId'";

$result = @mysql_query($query2);
if($result){
echo'<p align=center><font color="#333333"><b>UPDATED SUCCESSFULLY</b></font></p>';
}

else{
echo'<h1> System Error </h1>';
}

in below my $query2 stmt. Everytime I click on an image now it just deletes the product name each time without me choosing to do it. Is there a problem with a loop or something here?? Really grateful for your help!

Fumigator
08-30-2006, 05:59 PM
It looks like you are updating prodName but I can't see you assigning anything to $prodName, so it's probably wiping it out.

I still don't understand why you're updating.

ciaracous
09-02-2006, 05:08 PM
What I am trying to do is create a page where an administrator of a shop can go in and update the details such as price, name of products. What I want to happen is when the administrator clicks on an image, it displays all of the details on that product in a table. The administrator can then see all of the details and update anything that he or she wants to. I am now using the code that I used before for updating the records, but the only difference is before I used a submit button, and now I am allowing the user just to click on the image.

prodName is the first field that the admin can update, they cannot update the shopName or prodId.

The same thing is happening now, where the prodName is still being wiped out. It is also displaying the message "updated successfully" but this is before I even click on the update button. When I click on the update button it displays "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

Here is the code I am trying to use now. I'm really struggling with this and have a deadline to meet now!



<?php #Script 7.3 - register.php
include "db.php";
//if(isset($_GET)) echo '<pre>' . print_r($_GET,true) . '</pre>';


$prodId = 0; // UPDATED
$prodId = $_GET['prodId'];



$sql= "select * from product where prodId=$prodId";


$result = mysql_query($sql,$conn) or die(mysql_error()); //comment out if not working

if (mysql_num_rows($result) == 1) {

//if authorized, get the values of prodId, shopName...

$prodId = mysql_result($result, 0, 'prodId');
$shopName = mysql_result($result, 0, 'shopName');
$dept = mysql_result($result, 0, 'dept');
$brand = mysql_result($result, 0, 'brand');
$type = mysql_result($result, 0, 'type');
$image = mysql_result($result, 0, 'image');
$price = mysql_result($result, 0, 'price');





$query2 = "UPDATE product SET prodName='$prodName', dept='$dept', brand='$brand', type='$type', image='$image', price='$price' WHERE prodId ='$prodId' and shopName='$shopName'";



$result = @mysql_query($query2);
if($result){
echo'<p align=center><font color="#333333"><b>UPDATED SUCCESSFULLY</b></font></p>';
}

else{
echo'<h1> System Error </h1>';
}



$query = "SELECT * FROM product WHERE prodId = '$prodId' and shopName='$shopName'";


$result = @mysql_query($query);
if($result){
echo'


<form action="admin_update2.php" method="post">



<center>
<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">';

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo'


<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Product Id </b></center></td>
<td align="left">'.$row['prodId'].'</td>




<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Shop Name</b></td>
<td align="left">'.$row['shopName'].'</td>

<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Product Name</b></td>
<td align="left"><input type="text" name="prodName" size="30" maxlength="30" value="'.$row['prodName'].'"/></td>

<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Department</b></td>
<td align="left"><input type="text" name="dept" size="20" maxlength="20" value="'.$row['dept'].'"/></td>

//........

<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Price</b></td>
<td align="left"><input type="text" name="price" size="50" maxlength="50" value="'.$row['price'].'"/></td>



</tr>';
}
echo'


<tr><td><center><input type="submit" name="submit" value="UPDATE"/>
<input type="reset" value="CLEAR FORM"></p></center>


<input type="hidden" name="prodId" value='.$prodId.'>
<input type="hidden" name="shopName" value='.$sName.'>



<input type="hidden" name="submittedUpdate" value="TRUE"/></form></td></tr>

</table>

';
}

}

if(isset($_POST['submitted'])){

$prodId = trim($_POST['prodId']);
$shopName= $_GET['shopName'];





$query = "SELECT * FROM product WHERE prodId = '$prodId' and shopName='$shopName'";




$result = @mysql_query($query);
if($result){
echo'


<form action="admin_update.php" method="post">



<center>
<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8">';

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo'


<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Product Id</b></center></td>
<td align="left">'.$row['prodId'].'</td>



<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Shop Name</b></td>
<td align="left">'.$row['shopName'].'</td>

<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Product Name</b></td>
<td align="left"><input type="text" name="prodName" size="30" maxlength="30" value="'.$row['prodName'].'"/></td>

<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Department</b></td>
<td align="left"><input type="text" name="dept" size="20" maxlength="20" value="'.$row['dept'].'"/></td>

//.....

<tr><td align="left" bgcolor="#2696b8"><center><font color="black"><b>Price</b></td>
<td align="left"><input type="text" name="price" size="50" maxlength="50" value="'.$row['price'].'"/></td>



</tr>';
}
echo'


<tr><td><center><input type="submit" name="submit" value="UPDATE"/>
<!--<input type="reset" value="CLEAR FORM"></p></center>-->
<tr><td><center>



<input type="hidden" name="prodId" value='.$prodId.'>
<input type="hidden" name="shopName" value='.$shopName.'>



<input type="hidden" name="submittedUpdate" value="TRUE"/></form></td></tr>

</table>
';

}

else{
echo'<h1> System Error1 </h1> table ';
exit();
}

}
mysql_close();

?>


In my db I also have a field QtyInStock, which I have not included to update. Would this have anything to do with it??

ciaracous
09-02-2006, 05:22 PM
I actually changed



$query2 = "UPDATE product SET prodName='$prodName', dept='$dept', brand='$brand', type='$type', image='$image', price='$price' WHERE prodId ='$prodId' and shopName='$shopName'";


to



$query2 = mysql_query("UPDATE product SET prodName='$prodName', dept='$dept', brand='$brand', type='$type', image='$image', price='$price' WHERE prodId ='$prodId' and shopName='$shopName'");


and changed



$result = @mysql_query($query2);
if($result){
echo'<p align=center><font color="#333333"><b>UPDATED SUCCESSFULLY</b></font></p>';
}

else{
echo'<h1> System Error </h1>';
}

to


$result = @mysql_query($query2) or die("Unable to perform query <code>" . $query2 . "</code><br /><br />" . mysql_error());


Now it returns the error

Unable to perform query 1

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

Any idea what that means?

Fumigator
09-02-2006, 06:03 PM
You're trying to perform a query on $query2, which you are assigning the result of a query to. The result of a query is a special variable, what they call a resource.

Your bigger problem is your logic flow. You need to take a look at the overall picture again and then design the code from there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum