Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/195382-help-with-query/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/195382-help-with-query/#findComment-1026720
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/195382-help-with-query/#findComment-1026727
Share on other sites

Hi StathisG, thanks for the reply. I put your code in and it works but it updates every row. Here's my page code:

 

<?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

Link to comment
https://forums.phpfreaks.com/topic/195382-help-with-query/#findComment-1026728
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/195382-help-with-query/#findComment-1026762
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.