justAnoob Posted May 8, 2009 Share Posted May 8, 2009 little script here to edit mysql row data,,It will echo "Thank you! Information updated." But when I check my database,, the info never changed... Also, check the commented line below. <?php include "connection.php"; $id=$r["id"]; if ($_POST["submit"]) { $name = $_POST["item_name"]; $description = $_POST["description"]; $seeking = $_POST["in_return"]; $sql = "UPDATE $table1 SET item_name = '$name' WHERE id=$id"; $result = mysql_query($sql); echo "Thank you! Information updated."; echo $id; // tried to echo the row id,, but nothing comes up } ?> Here is one of the input boxes that I have on the form. <?php <input type="name" value="' . $row['item_name'] . '" size="50" /> ?> Quote Link to comment Share on other sites More sharing options...
xtopolis Posted May 8, 2009 Share Posted May 8, 2009 Your HTML is broken. Your <input> example has an incorrect type value. http://www.w3schools.com/TAGS/tag_input.asp You probably wanted something like this: <input type="type" name="item_name" value="<?php echo $row['item_name']; ?>" size="50" /> Also, I don't know where $r['id'] comes from, so I can't really help you there.. As for the "thank you" message being display, you could add a check to see how many rows where updated and display it only if a row was successfully changed, rather than after it queries (regardless of success). Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 8, 2009 Author Share Posted May 8, 2009 Still having problems,,, can anyone help,, this is driving me crazy... I search everywhere for an answer and nothing. <?php session_start(); include "connection.php"; $getthatname = $_REQUEST['item_name']; $gotit = $getthatname; if ($_POST["submit"]) { $item_name = mysql_real_escape_string($_POST["item_name"]); $description = mysql_real_escape_string($_POST["description"]); $in_return = mysql_real_escape_string($_POST["in_return"]); mysql_query("UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'"); echo "$gotit has been updated."; mysql_close(); } ?> Here is the table that I'm working from......And yes my table is inside a form(form2) and my action is set.... The script echos that it was updated. But when I return to my table on my site,,, the values of the database have not been changed... Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 1. Possible SQL injection on $gotit. 2. Change this - mysql_query("UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'"); to mysql_query("UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'") or die(mysql_error()); Any errors from that? Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 8, 2009 Author Share Posted May 8, 2009 No, there are no errors....I am so confused.... Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Can you post your form? And I assume abcxyz is your table name. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 8, 2009 Author Share Posted May 8, 2009 ooppsss,, forgot to post that,,, one minute. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 8, 2009 Author Share Posted May 8, 2009 <form id="form2" name="form2" method="post" action="updatetrade.php"> <?php session_start(); include "connection.php"; $findit = $_SESSION['id']; $result = mysql_query("SELECT id FROM members WHERE username = '$findit'"); $result = mysql_result($result, 0, 0); $sql = mysql_query("SELECT imgpath, imgpath2, imgpath3, imgpath4, imgpath5, item_name, description, in_return FROM abcxyz WHERE user_id = '$result'"); $img2query = mysql_query("SELECT imgpath2 FROM abcxyz WHERE user_id = '$result'"); $num_rows = mysql_num_rows($img2query); echo "<table width='954' height='523' border='0' align='center' cellpadding='0' cellspacing='0' bordercolor='#000000' bgcolor='#BBD4E1'>"; while ($row = mysql_fetch_array($sql)) { echo "<tr><td width='188' height='180'><div align='center'>"; echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />'; echo "</div></td><td width='188'><div align='center'>"; echo '<img src="' . $row['imgpath2'] . '" width="125" alt="" />'; echo "</div></td><td width='188'><div align='center'>"; echo '<img src="' . $row['imgpath3'] . '" width="125" alt="" />'; echo "</div></td><td width='188'><div align='center'>"; echo '<img src="' . $row['imgpath4'] . '" width="125" alt="" />'; echo "</div></td><td width='190'><div align='center'>"; echo '<img src="' . $row['imgpath5'] . '" width="125" alt="" />'; echo "</div></td></tr><tr><td height='43' colspan='4'>"; echo '<strong>Item Name:</strong> <input type="text" name="item_name" value="' . $row['item_name'] . '" size="50" />'; echo "</td><td><div align='center'>"; echo '<input type="submit" name="submit" id="submit" value="Update Trade">'; echo "</div></td></tr><tr><td height='116' colspan='4'>"; echo '<strong>Description:</strong> <textarea name="description" cols="75" rows="5">' . $row['description'] . '</textarea>'; echo "</td><td><div align='center'>"; echo "</div></td></tr><tr><td height='124' colspan='4'>"; echo '<strong>Seeking:</strong> <textarea name="in_return" cols="75" rows="5">' . $row['in_return'] . ' </textarea>'; echo "</td><td><div align='center'>"; echo "</div></td></tr><tr><td colspan='4'>"; //echo "save delete buttons"; echo "</td><td><div align='center'>"; echo "</td></tr>"; } echo "</table>"; ?> </form> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 1. Is there a reason you're alternating single quotes and double quotes for the HTML echo? For xHTML-compliancy, you should start echo with a single quote and double quote the attribute of the HTML tag. 2. What's the purpose of this: $img2query = mysql_query("SELECT imgpath2 FROM abcxyz WHERE user_id = '$result'"); $num_rows = mysql_num_rows($img2query); 3. What's the problem you're having? I don't see an UPDATE query. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 8, 2009 Author Share Posted May 8, 2009 the script that is inside the form is not the problem.. everything works fine there... it is just the mysql update script that I posted earlier. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 8, 2009 Author Share Posted May 8, 2009 those 2 lines that you mentioned shouldn't have been there.. that was from something that I was working on earlier... there gone now.... here you go... <form id="form2" name="form2" method="post" action="updatetrade.php"> <?php session_start(); include "connection.php"; $findit = $_SESSION['id']; $result = mysql_query("SELECT id FROM members WHERE username = '$findit'"); $result = mysql_result($result, 0, 0); $sql = mysql_query("SELECT imgpath, imgpath2, imgpath3, imgpath4, imgpath5, item_name, description, in_return FROM abcxyz WHERE user_id = '$result'"); echo "<table width='954' height='523' border='0' align='center' cellpadding='0' cellspacing='0' bordercolor='#000000' bgcolor='#BBD4E1'>"; while ($row = mysql_fetch_array($sql)) { echo "<tr><td width='188' height='180'><div align='center'>"; echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />'; echo "</div></td><td width='188'><div align='center'>"; echo '<img src="' . $row['imgpath2'] . '" width="125" alt="" />'; echo "</div></td><td width='188'><div align='center'>"; echo '<img src="' . $row['imgpath3'] . '" width="125" alt="" />'; echo "</div></td><td width='188'><div align='center'>"; echo '<img src="' . $row['imgpath4'] . '" width="125" alt="" />'; echo "</div></td><td width='190'><div align='center'>"; echo '<img src="' . $row['imgpath5'] . '" width="125" alt="" />'; echo "</div></td></tr><tr><td height='43' colspan='4'>"; echo '<strong>Item Name:</strong> <input type="text" name="item_name" value="' . $row['item_name'] . '" size="50" />'; echo "</td><td><div align='center'>"; echo '<input type="submit" name="submit" id="submit" value="Update Trade">'; echo "</div></td></tr><tr><td height='116' colspan='4'>"; echo '<strong>Description:</strong> <textarea name="description" cols="75" rows="5">' . $row['description'] . '</textarea>'; echo "</td><td><div align='center'>"; echo "</div></td></tr><tr><td height='124' colspan='4'>"; echo '<strong>Seeking:</strong> <textarea name="in_return" cols="75" rows="5">' . $row['in_return'] . ' </textarea>'; echo "</td><td><div align='center'>"; echo "</div></td></tr><tr><td colspan='4'>"; //echo "save delete buttons"; echo "</td><td><div align='center'>"; echo "</td></tr>"; } echo "</table>"; ?> </form> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Oh then first question. What is this: $id=$r["id"]; I don't see $r defined anywhere. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 8, 2009 Author Share Posted May 8, 2009 that is old,,, got in my post somehow by accident... this is the new script..... <?php session_start(); include "connection.php"; $getthatname = $_REQUEST['item_name']; $gotit = $getthatname; if ($_POST["submit"]) { $item_name = mysql_real_escape_string($_POST["item_name"]); $description = mysql_real_escape_string($_POST["description"]); $in_return = mysql_real_escape_string($_POST["in_return"]); mysql_query("UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'") or die( mysql_error()); echo "$gotit has been updated."; mysql_close(); } ?> I get no errors,, and it echos that it is updated.. But when I go back to my page that has my table with the mysql info.. everything is the same.. So somewher in this script it is not posting the information that I changed on my form. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted May 9, 2009 Share Posted May 9, 2009 Try this: <?php error_reporting(E_ALL); ini_set('display_errors',1); session_start(); require 'connection.php'; if(isset($_POST['submit'])) { $item_name = mysql_real_escape_string($_POST['item_name']); $description = mysql_real_escape_string($_POST['description']); $in_return = mysql_real_escape_string($_POST['in_return']); $sql = "UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$item_name'"; if(mysql_query($sql)) { echo $item_name . ' has been updated.'; }else{ echo 'There was an error processing the query: ' . mysql_error(); } }else{//if form has been submitted to this page echo '//maybe you could put form code here'; } ?> Don't use $_REQUEST as it's depreciated. Technically if there was an error, ken's previous mysql modification would have caught it, but perhaps you don't have error reporting on. If you still don't see error after this, try pasting that mysql query directly into your mysql console / phpmyadmin and see what it say (with values filled in of course). Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 9, 2009 Author Share Posted May 9, 2009 Nope nothing yet... Still says that is was updated,, but I return to my form table page and everthing is the same. (also checked mysql database for fun,, no change there either. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted May 9, 2009 Share Posted May 9, 2009 Did you try my last suggestion? If you still don't see errors after this, try pasting that mysql query directly into your mysql console / phpmyadmin and see what it say (with values filled in of course). Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 9, 2009 Author Share Posted May 9, 2009 Not sure what you mean... Could you explain. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 9, 2009 Author Share Posted May 9, 2009 If you mean to execute this line inside of mysql $sql = "UPDATE member_trades SET item_name = 'blah', description = 'blah blah', in_return = '$blah blah blah' WHERE item_name = '$item_name'"; I got an error saying to check your sql syntax that corresponds with mysql version Quote Link to comment Share on other sites More sharing options...
xtopolis Posted May 9, 2009 Share Posted May 9, 2009 So you have this query: mysql_query("UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit'") But yet the database does not update, even when it says the query ran, according to PHP. So take this query line: UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit' CHANGE ALL $values to be valid data, DO NOT LEAVE THIS ABOVE LINE AS IS, FIX IT TO CONTAIN REAL VALUES And paste it into your phpMyAdmin sql interface, or a mysql console screen if it's hosted by you... What I'm saying is, don't use PHP, go directly to MYSQL and test that query to make sure it's doing what you expect so that we can rule out mysql as the problem. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 9, 2009 Author Share Posted May 9, 2009 this line worked inside mysql interface UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit' Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 9, 2009 Author Share Posted May 9, 2009 I take that back,,, it says that the affected rows = 0,,,, so it did nothing. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 9, 2009 Author Share Posted May 9, 2009 ok,, this works now in mysql interface,,, when I fill in some values UPDATE abcxyz SET item_name = '$item_name', description = '$description', in_return = '$in_return' WHERE item_name = '$gotit' Quote Link to comment Share on other sites More sharing options...
xtopolis Posted May 9, 2009 Share Posted May 9, 2009 Ok, now post the working example back into php. Do not use the $variables, use the values (change 1) to verify that it makes it from PHP -> MySQL with a working query. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 9, 2009 Author Share Posted May 9, 2009 alright,, I posted that back into php and put some new values and that worked out great...So I guess the problem is somewhere in the variables from my text field or text boxes........or the problem is where I'm selecting the item_name. Quote Link to comment Share on other sites More sharing options...
justAnoob Posted May 9, 2009 Author Share Posted May 9, 2009 Well,,, it works kinda... I can update and change the description and the in_return fields as long as I don't change the item_name... Why,, well when I change the item_name the script is looking for the changed item_name in my database which it won't find...So I'll try and work it out now.. If anyone has any ideas,,, please feel free... And a big thanks to everyone that helped. Quote Link to comment 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.