KingSpongo Posted March 16, 2010 Share Posted March 16, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/195382-help-with-query/ Share on other sites More sharing options...
StathisG Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195382-help-with-query/#findComment-1026720 Share on other sites More sharing options...
Maq Posted March 16, 2010 Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195382-help-with-query/#findComment-1026727 Share on other sites More sharing options...
KingSpongo Posted March 16, 2010 Author Share Posted March 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195382-help-with-query/#findComment-1026728 Share on other sites More sharing options...
StathisG Posted March 16, 2010 Share Posted March 16, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/195382-help-with-query/#findComment-1026762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.