Author Topic: Help with query  (Read 277 times)

0 Members and 1 Guest are viewing this topic.

Offline KingSpongoTopic starter

  • Irregular
  • Posts: 15
    • View Profile
Help with query
« on: March 15, 2010, 08:11:24 PM »
Hey all,

I have a table that contains: product_id, product_name, product_description and user_name.
This is displayed in a html table. Sometimes the user_name field might be empty because their will be no user associated with a product. I have made an if statement that if user_name is NULL to display a button. When the button is pressed I want the query to update the user_name column with a session I made "$_SESSION['uid']". I don't know how to do this. I have attempted it many times but can't get it to work. Any help?

Thank you

Offline StathisG

  • Irregular
  • Posts: 24
  • Gender: Male
    • View Profile
    • Portfolio
Re: Help with query
« Reply #1 on: March 15, 2010, 08:32:40 PM »
Somehow, you'll need to send the product_id to the page that will update the table. Let's assume that you'll send it using GET:


$productId 
mysql_real_escape_string ($_GET['product_id']);
$newUserName $_SESSION['uid'];

$query "UPDATE table_name SET user_name='$newUserName' WHERE product_id=$productId";


Also, you'll get more specific answers in the future if you include some of your code.

Offline Maq

  • Global Moderator
  • 'Insane!'
  • *
  • Posts: 11,004
  • Gender: Male
    • View Profile
    • Top Ecigs Reviews
Re: Help with query
« Reply #2 on: March 15, 2010, 08:55:44 PM »
Hi KingSpongo,
What part do you need help with exactly?  There are multiple parts to the overall question.  I'm going to assume you're referring to the SQL syntax in which StathisG would be correct if, presumably, 'product_id' is an integer.  Otherwise you're going to have to use single quotes around the value.
Electronic Cigarette Reviews - Smoking alternatives, find YOUR ecig!
ini_set ("display_errors""1");
error_reporting(E_ALL);

Offline KingSpongoTopic starter

  • Irregular
  • Posts: 15
    • View Profile
Re: Help with query
« Reply #3 on: March 15, 2010, 08:57:26 PM »
Hi StathisG, thanks for the reply. I put your code in and it works but it updates every row. Here's my page code:

Code: [Select]
<?php session_start();
$con mysql_connect("localhost","root","password");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }
?>

<html>
<head></head>
<body>


<?php
mysql_select_db
("test2"$con);

$result mysql_query("SELECT * FROM product");


echo 
"<table border='1'>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Product Description</th>
<th>Availability</th>
<th>User Name</th>
<th>Reserve</th>
</tr>"
;

while(
$row mysql_fetch_array($result))
  {
  echo 
"<tr>";
  echo 
"<td>" $row['product_id'] . "</td>";
  echo 
"<td>" $row['product_name'] . "</td>";
  echo 
"<td>" $row['product_description'] . "</td>";
  
  if (
$row['user_name']==NULL)

echo '<td><font color="Green">Available</font></td>';
else
echo '<td><font color="Red">Reserved</font></td>';
  
  
  echo 
"<td>" $row['user_name'] . "</td>";
  
  
  if($_POST['submit']){
$productId mysql_real_escape_string ($_GET['product_id']);
$newUserName $_SESSION['uid'];
$query "UPDATE product SET user_name='$newUserName' WHERE product_id=$productId";


}
  echo 
"<form method=\"POST\" action=\"\">";
if ($row['user_name']==NULL){
echo "<td><input type=\"submit\" value=\"submit\" id=\"submit\" name=\"submit\" /></td>";
    }
  echo 
"</form>";
echo 
"</tr>";
  }
echo 
"</table>";



mysql_close($con);
?>

</body>
</html>

Can you help me fix this? I appreciate any given help.  :D

Offline StathisG

  • Irregular
  • Posts: 24
  • Gender: Male
    • View Profile
    • Portfolio
Re: Help with query
« Reply #4 on: March 15, 2010, 10:07:01 PM »
The purpose is to think a little bit and alter the examples given to your code. Not copy-paste everything.

Anyway, from a quick look I see that you're not passing the product_id. Inside your form you need to include the product_id. An example:


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


Then, you have to grab this value (you are using post and not get as I can see so you should pay attention to what you are copying..):

if(isset($_POST['submit']))
{
  
$productId mysql_real_escape_string ($_POST['product_id']);
}


If you try a little bit you're going to figure it out :)