radiohomer Posted April 22, 2011 Share Posted April 22, 2011 hey guys i have this code FILE NAME "index.php" <?php // make a connection to the database mysql_connect ("localhost", "root", "vertrigo") or die ('Error: I Failed To Connect To The Database ' . mysql_error()); mysql_select_db ("test"); // Get Data $query = mysql_query("SELECT * FROM TestTable"); // display the data and loop while ($row = mysql_fetch_array($query)) { echo "<br /> ID: ".$row['ID']."<br /> First Name: ".$row['FName']."<br /> Last Name: ".$row['LName']."<br /> Contact Number: ".$row['CNumber']."<br />";} ?> <form method="post" action="update.php"> <table border="1" align="center"> <tr> <td align="right" width="220">ID: </td> <td align="left" width="220"> <input type="text" name="ID" size="30" /></td> </tr> <tr> <td align="right" width="220">First Name: </td> <td align="left" width="220"> <input type="text" name="FName" size="30" /></td> </tr> <tr> <td align="right" width="220">Last Name: </td> <td align="left" width="220"> <input type="text" name="LName" size="30" /></td> </tr> <tr> <td align="right" width="220">Contact Number: </td> <td align="left" width="220"> <input type="text" name="CNumber" size="30" /></td> </tr> <tr> <td align="right" width="220"><input type="reset" value="Reset" /> </td> <td align="left" width="220"> <input type="submit" value="Update Database" /></td> </tr> </table> </form> and i also have this code FILE NAME "update.php" <?php $ID = $_POST['ID']; $FName = $_POST['FName']; $LName = $_POST['LName']; $CNumber = $_POST['CNumber']; mysql_connect ("localhost", "root", "vertrigo") or die ('Error: I Failed To Connect To The Database ' . mysql_error()); mysql_select_db ("test"); $query="INSERT INTO testtable (ID, FName, LName, CNumber)VALUES ('".$ID."','".$FName."', '".$LName."', '".$CNumber."')"; mysql_query($query) or die ('Error Updating Database'); echo "Database Updated Sucsessfully With: ".$ID." ".$FName." ".$LName." ".$CNumber ; ?> ok so the script is working like a charm its sending the data to the database as i want it to. the problem i have is that i want to be able to update the info that is already on the database lets say i want to change a phone/contact number i have typed the ID number into the ID text field and the same first and last name into there correct boxes and then typed in the new phone number i then click submit and i get the error ""Error Updating Database"" i have looked all over the forum and net to see what i have done wrong to not allow this code to update can anyone help me out here please im quite new to the php language and could really do with some pointers thanks Steve Quote Link to comment https://forums.phpfreaks.com/topic/234477-overwrite-data-stored-on-a-db/ Share on other sites More sharing options...
NLCJ Posted April 22, 2011 Share Posted April 22, 2011 If you want to update something you will have to use UPDATE in the mysql string. Example± mysql_query("UPDATE `users` SET `option1`='value', `option2`='value2' WHERE `condition`='something'"); So, add an if where you check if there is already an entry in the database, if it is; update otherwise insert. Quote Link to comment https://forums.phpfreaks.com/topic/234477-overwrite-data-stored-on-a-db/#findComment-1205064 Share on other sites More sharing options...
radiohomer Posted April 22, 2011 Author Share Posted April 22, 2011 so how would i place that code onto the file i have again i dont know much about php and im still learning would i just put that code into the update.php or do i need to change / add things to it? it would be very helpful if you could insert the values to check "FName" "LName" "CNumber" or maybe a guide or something i can follow Quote Link to comment https://forums.phpfreaks.com/topic/234477-overwrite-data-stored-on-a-db/#findComment-1205085 Share on other sites More sharing options...
radiohomer Posted April 23, 2011 Author Share Posted April 23, 2011 ok it doesnt matter i have sorted this out thanks Quote Link to comment https://forums.phpfreaks.com/topic/234477-overwrite-data-stored-on-a-db/#findComment-1205189 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.