Jump to content

prod_id not populating correctly in tblretprod


hance2105

Recommended Posts

hi guys, i have 2 tables - tblproduct and tblretprod.
i insert my product details to tbl product with prod_id being auto increment
other fields are prod_name, prod_brand, prod_desc, prod_photo, cat, subcat

i hv another table tblretprod with fields id, user_id, prod_id, prod_price

i can add my products successfully

i am displaying all the products i added in a table i echo along with a textfield to enter the product price and an add button

on clicking the add button, user_id for the session, prod_id and prod_price should be inserted in the tblretprod

user_id and prod_price are being inserted correctly, but the prod_id which is unique to each product is not being added. only the first prod_id i.e 1 is being added everywhere

here are my codes to add the product when i click on add

 

<?php 

session_start(); 

include('db_connect.php'); 

$username = $_SESSION['username']; 

$prod_price = $_POST['prod_price']; 

$url='retprod.php'; 

$sql=mysql_query("select user_id from tbllogin where username = '$username'"); 

$row = mysql_fetch_array($sql); 

$sql1 = mysql_query("select * from tblproduct"); 

$row1 = mysql_fetch_array($sql1); 

$sql2 = mysql_query("insert into tblretprod (user_id, prod_id, prod_price) values ('$row[user_id]', '$row1[prod_id]', '$prod_price')"); 


    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; 
    echo "<script>alert('This product has been added successfully.')</script>"; 

?>

You are selecting one product from tblproduct and you keep re-entering the same prod_id.  What you need to do is on your form make a hidden value with the products id

<?php 


session_start(); 


include('db_connect.php'); 

$username = $_SESSION['username']; 

$prod_price = mysql_real_escape_string($_POST['prod_price']); 

$prod_id = mysql_real_escape_string($_POST['prod_id']);

$url = 'retprod.php'; 

$user = mysql_fetch_assoc(mysql_query("select user_id from tbllogin where username = '{$username}'"));

$insert = mysql_query("insert into tblretprod (user_id, prod_id, prod_price) values ('{$user[user_id]}', '{$prod_id}', '{$prod_price}')"); 

if($insert){

     $_SESSION['INSERTSUCCESS'] = true;

}
else{

     $_SESSION['INSERTSUCCESS'] = false;

}

header("Location: /" . $url);

?>

You could even make it more fancy and if a product price didnt update you can pass numbers through the session insertsuccess like 0 if it does not fail and the prod_id if it fails so you can put and error next to the one that had the problem updating.... if you do not understand let me show you

your script

<?php 


session_start(); 


include('db_connect.php'); 

$username = $_SESSION['username']; 

$prod_price = mysql_real_escape_string($_POST['prod_price']); 

$prod_id = mysql_real_escape_string($_POST['prod_id']);

$url = 'retprod.php'; 

$user = mysql_fetch_assoc(mysql_query("select user_id from tbllogin where username = '{$username}'"));

$insert = mysql_query("insert into tblretprod (user_id, prod_id, prod_price) values ('{$user[user_id]}', '{$prod_id}', '{$prod_price}')"); 

if($insert){

     $_SESSION['INSERTSUCCESS'] = 0;

}
else{

     $_SESSION['INSERTSUCCESS'] = $prod_id;

}

header("Location: /" . $url);

?>

your form

<?php

//you can also make it that if the price is already set it overrides this one....
//I used sample id's
?>
<form action='' method='post'>
{Product Info Here}
<input type="text" name="prod_price" value="" />
<input type="hidden" name="prod_id" value="37498" />
<input type="submit" name="prod_submit" value="add" />
<?php
if($_SESSION['INSERTSUCCESS'] == '37498'){
    echo 'Error Updating Price!';
}
?>

you missed this suggestion -

 

 

What you need to do is on your form make a hidden value with the products id

 

it's your form that is sending the prod_id and prod_price. how else would you know which product the retailer selected and entered his price for?

never did this before so is this right?


<input type="hidden" name="prod_id" value="' . $row['prod_id'] . '" />

and where i insert it? should i echo it here?


while($row=mysql_fetch_assoc($sql)){
extract($row);
 
echo "<tr>";
echo '<td><center><img height="100" width="100" src="Images/Products/'.$row['prod_photo'].'"/></td>';
echo "<td><center>".$row['prod_name']."</td>";
echo "<td><center>".$row['prod_brand']."</td>";
echo "<td>"."<form name=\"price\" method=\"post\" action=\"ret_addprod.php\">" .
"<center><input type=\"text\" name=\"prod_price\" />" .
"<input type=\"submit\" value=\"Add\" /></form>"."<td>";

while($row = mysql_fetch_assoc($sql)){
     extract($row); //you are calling extract, but your not using it...
     //in you <img you should have an alt always.
     //that tells the google spider what the image is
     echo <<<EOT
     <tr>
      <td style="text-align: center;">
       <img height="100" width="100" src="Images/Products/{$prod_photo}" alt="{$prod_name}" />
      </td>
      <td style="text-align: center;">
       {$prod_name}
      </td>
      <td style="text-align: center;">
       {$prod_brand}
      </td>
      <td>
       <form name="price" method="post" action="ret_addprod.php">
        <input type="hidden" name="prod_id" value="{$prod_id}" />
        <input type="text" name="prod_price" />
        <input type="submit" value="Add" />
       </form>
      </td>
     </tr>
EOT;
}

I just cleaned it up a little bit and threw in the hidden value, and used the extract function

thanks for the help but i am using the deprecated SQL codes :( i know i have to learn the new one but my project nearly over so won't have time for that for now

 

i tried to rewrite code this way but there is some error somewhere and am not able to find it

 

while($row = mysql_fetch_assoc($sql)){     extract($row);       echo "<tr>";     echo '<td style="text-align:center;"><img height="100" width="100" src="Images/Products/'.$row['prod_photo'].'" alt="'.$row['prod _name'].'" /></td>';      echo "<td style="text-align: center;">".$row['prod_name']."</td>";      echo "<td style="text-align: center;">".$row['prod_brand']."</td>";      echo "<td>"."<form name=\"price\" method=\"post\" action=\"ret_addprod.php\">".        "<input type=\"hidden\" name=\"prod_id\" value=\"'.$row['prod_id'].'\" />.        "<center><input type=\"text\" name=\"prod_price\" />.        "<input type=\"submit\" value=\"Add\" /></form>"."</td>";    } some help would be a welcome guys

ok i managed to sort this one out 

 

i have below code but still only the first prod_id is being added everywhere

 

while($row = mysql_fetch_assoc($sql)){     extract($row);       echo "<tr>";     echo '<td style="text-align:center;"><img height="100" width="100" src="Images/Products/'.$row['prod_photo'].'"/></td>';      echo "<td style='text-align: center;'>".$row['prod_name']."</td>";      echo "<td style='text-align: center;'>".$row['prod_brand']."</td>";      echo "<td>"."<form name=\"price\" method=\"post\" action=\"ret_addprod.php\">".        "<input type=\"hidden\" name=\"prod_id\" value=".$row['prod_id']." />".        "<input type=\"text\" name=\"prod_price\" />".        "<input type=\"submit\" value=\"Add\" /></form>"."</td>";    }

i have below code but still only the first prod_id is being added everywhere

 

okay, so when you were debugging what your code is doing, what did you find out? where are the prod_id values what you expect and at what point do they all change to the first prod_id value? the error in your logic would be at the point where they all change to the first prod_id.

well i have a products table with product details stored. i am echoing the product photo, name and brand from that table and am also echoing a text field with an add button to be able to insert the product prices. the prod_id values are stored in that products table.

 

i have another table called tblretprod which will store user_id, the prod_id of the product being echoed and the price that will be set.

 

for now, only the user_id of the user is being stored and the price i am inserting in the text field. but the prod_id being stored is only that of the first product

 

am unable to know where the error is occurring for only the first prod_id is being inserted in the myql table

hmmm am thinking if i wrote the add code well, can anyone please verify and let me know? perhaps this is the cause of my issue

 

<?php session_start(); include('db_connect.php'); $username = $_SESSION['username']; $prod_price = $_POST['prod_price']; $url='retprod.php'; $sql=mysql_query("select user_id from tbllogin where username = '$username'"); $row = mysql_fetch_array($sql); $sql1 = mysql_query("select * from tblproduct"); $row1 = mysql_fetch_array($sql1); $sql2 = mysql_query("insert into tblretprod (user_id, prod_id, prod_price) values ('$row[user_id]', '$row1[prod_id]', '$prod_price')");      echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';echo "<script>alert('This product has been added successfully.')</script>"; ?> 

Archived

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

×
×
  • Create New...

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.