Lee-Bartlett Posted September 24, 2008 Share Posted September 24, 2008 Ok i have my database with 1 table in it, say i wanna edit a record which has been submited, say because it is stupid or typoed etc. I would like to be able to update it with text feilds, say name is spelt wrong, i have a text feild where i typed the correct name in and it updates that row of information. How would i go about doing this, any nice tuts or some advice is great. It can be very simple nothing advanced Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/ Share on other sites More sharing options...
wildteen88 Posted September 24, 2008 Share Posted September 24, 2008 Your first the pull whatever row you want to edit from the database, you'll place each column within a form field. When you submit the form you just run an UPDATE query, eg UPDATE TABLE `table_here` SET col1name=`$col1value`, col12name=`$col2value` etc WHERE row_id_col=$rowid Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-649624 Share on other sites More sharing options...
Lee-Bartlett Posted September 24, 2008 Author Share Posted September 24, 2008 This i what i had so far, <?php mysql_select_db("roberts_work", $connect); mysql_query("UPDATE tblbasicform SET location = 'Harlow' WHERE name = 'chris' AND email = '[email protected]'"); mysql_close($connect); ?> But i need it to work in a form, for example, i have a db with email and name in, i search the users by email. On my page i would like 2 text feilds and an update button, when i fill out them 2 feileds, what ever user with that name, is updated with the text i put in. Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-649636 Share on other sites More sharing options...
hcdarkmage Posted September 24, 2008 Share Posted September 24, 2008 If you have setup a unique primary key in your database, you could use it for updating the information. Create the form that has the fields you want to update the database: <form name="form1" method="post"> Input First Name: <input type="text" name="firstname" /><br /> Input Email: <input type="text" name="email" /><br /> Input Location: <input type="text" name="location" /><br /> Input ID Number: <input type="text" name="id" /><br /> <input type="submit" name="submit" value="Update Data" /> </form> Of course being a little rusty, in the PHP section of the page you do something like this: <?php foreach($_POST as $key => $value) $$key = $value; //This sets the form field names to values. mysql_query("UPDATE table SET location = '$location', name = '$firstname', email = '$email' WHERE id = '$id'") or die(mysql_error()); ?> That way you have a clean update of any information that needs to be updated. Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-649726 Share on other sites More sharing options...
Lee-Bartlett Posted September 24, 2008 Author Share Posted September 24, 2008 I dont understand that completly, it doesnt update when i changed everything to. I would of thought first i would need a box where i search for name/id number, then if i get a result back it would let me use 4 text boxxes to update the rows i have. Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-649923 Share on other sites More sharing options...
hcdarkmage Posted September 24, 2008 Share Posted September 24, 2008 I dont understand that completly, it doesnt update when i changed everything to. I would of thought first i would need a box where i search for name/id number, then if i get a result back it would let me use 4 text boxxes to update the rows i have. You could do a drop down box that selects all the people in the database and sets the values of the form to auto-populate with the information and you can change and update the information. <?php $nameQry = "SELECT * FROM database"; $result = mysql_query($nameQry) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $name = $row['name']; $email = $row['email']; $location = $row['location']; $id = $row['id']; } ?> Then in your form control you have this: <form name="form1" method="post"> <select id="names" name="names"> <option>option values</option> </select> Input First Name: <input type="text" name="firstname" value="<? echo $name ?>" /><br /> Input Email: <input type="text" name="email" value="<? echo $email ?>" /><br /> Input Location: <input type="text" name="location" value="<? echo $location?>" /><br /> Input ID Number: <input type="text" name="id" value="<? echo $id?>" /><br /> <input type="submit" name="submit" value="Update Data" /> </form> or something along those lines. Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-649957 Share on other sites More sharing options...
Lee-Bartlett Posted September 25, 2008 Author Share Posted September 25, 2008 only problem with this is, the drop down stays on option value and the feilds fill straight away with the first row in my db, when i try change it or update it. Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-649981 Share on other sites More sharing options...
hcdarkmage Posted September 25, 2008 Share Posted September 25, 2008 Alright....lets try this: <?php $nameQry = "SELECT * FROM database"; $result = mysql_query($nameQry) or die(mysql_error()); $i = 0; while($row = mysql_fetch_array($result)){ $name = $row['name']; $email = $row['email']; $location = $row['location']; $id = $row['id']; $optBox[$i] = $row; $i++; } if(isset($names)){ $valueQry = "SELECT * FROM database WHERE id = '$id'"; $result = mysql_query($valueQry) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $valID = $row['id']; $valName = $row['name']; $valEmail = $row['email']; $valLocation = $row['location']; } } ?> Then in the form area: <form name="form1" method="post"> <select id="names" name="names"> <? for ($j=0; sizeof( $optBox ); j++){ echo "<option value='".$optbox[$j]['id']."'>".$optbox[$j]['name']."</option>"; } ?> </select> Input First Name: <input type="text" name="firstname" value="<? echo $valName ?>" /><br /> Input Email: <input type="text" name="email" value="<? echo $valEmail ?>" /><br /> Input Location: <input type="text" name="location" value="<? echo $valLocation?>" /><br /> Input ID Number: <input type="text" name="id" value="<? echo $valID?>" /><br /> <input type="submit" name="submit" value="Update Data" /> </form> This code is untested, but in theory should help. Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-650464 Share on other sites More sharing options...
Lee-Bartlett Posted September 25, 2008 Author Share Posted September 25, 2008 So close, the drop down has found the database and the form is filled in with the bottom entry of the database, dunno why. All i need now is the update bit. Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-650741 Share on other sites More sharing options...
Lee-Bartlett Posted September 26, 2008 Author Share Posted September 26, 2008 i forgot to put the form code bit in the code, now im reciving back Parse error: syntax error, unexpected T_INC, expecting ')' in C:\xampp\htdocs\Website\update.php on line 35 i took out a ; to see if that fixxed it and i get unexpected 2 string to Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-651418 Share on other sites More sharing options...
amagondes Posted September 26, 2008 Share Posted September 26, 2008 Hi, you are missing the $ infront of your variable j when incrementing it change: for ($j=0; sizeof( $optBox ); j++){ to: for ($j=0; sizeof( $optBox ); $j++){ Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-651588 Share on other sites More sharing options...
hcdarkmage Posted September 29, 2008 Share Posted September 29, 2008 Hi, you are missing the $ infront of your variable j when incrementing it change: for ($j=0; sizeof( $optBox ); j++){ to: for ($j=0; sizeof( $optBox ); $j++){ Thanks for catching that. Usually when I code, I go a bit fast and forget to put in the $ and ; every once in a while. It bugs the crap out of people when that happens, but it happens. Quote Link to comment https://forums.phpfreaks.com/topic/125643-how-would-i-got-about-this/#findComment-652966 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.