Russia Posted November 4, 2009 Share Posted November 4, 2009 I would like to make this script so that instead of inserting info it updates the code to update the first row. I thought of adding this to the update WHERE id='$id' and having this to say what row $id = $rows['id']; So something like this: <?php require('inc/config.php'); $id = $rows['id']; $sql="UPDATE Persons WHERE id='$id' (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } echo "1 record added"; ?> And the form: <html> <body> <form action="insert.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> Is that correct? and mostly will it work? Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/ Share on other sites More sharing options...
lemmin Posted November 4, 2009 Share Posted November 4, 2009 The WHERE clause should be after your values Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951164 Share on other sites More sharing options...
mrMarcus Posted November 4, 2009 Share Posted November 4, 2009 if you had tried it, you'd have figured out that it does not work. you are using UPDATE incorrectly: <?php $sql = "UPDATE `table` SET `field1` = '".$field1."', `field2` = '".$field2."' WHERE `somefield` = '".$somevariable."'"; ?> where is $rows being set? will that value be coming from another database query? EDIT: and always, always scrub/sanitize your incoming variables with mysql_real_escape_string() before running them through the query. UPDATE syntax usage of mysql_real_escape_string() would be: <?php $firstname = mysql_real_escape_string ($_POST['firstname']); //and so on for each incoming variable going to the db; ?> Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951167 Share on other sites More sharing options...
Russia Posted November 4, 2009 Author Share Posted November 4, 2009 So it will be like this? <?php include ('inc/config.php'); $id = $rows['id']; mysql_query("UPDATE Persons WHERE id='$id' SET FirstName = '$_POST[firstname]', LastName = '$_POST[lastname]', MiddleName = '$_POST[middlename]' "); ?> Is this correct? Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951172 Share on other sites More sharing options...
Russia Posted November 4, 2009 Author Share Posted November 4, 2009 Edit: Okay I have done that, will it work? <?php include ('inc/config.php'); $id = $rows['id']; mysql_query("UPDATE Persons WHERE id='$id' SET $FirstName = mysql_real_escape_string ($_POST['firstname']); $LastName = mysql_real_escape_string ($_POST['lastname']); $MiddleName= mysql_real_escape_string ($_POST['middlename']); "); ?> Is that correct? and will it work? Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951174 Share on other sites More sharing options...
mrMarcus Posted November 4, 2009 Share Posted November 4, 2009 So it will be like this? <?php include ('inc/config.php'); $id = $rows['id']; mysql_query("UPDATE Persons WHERE id='$id' SET FirstName = '$_POST[firstname]', LastName = '$_POST[lastname]', MiddleName = '$_POST[middlename]' "); ?> Is this correct? match your query to mine, and it becomes quite obvious that it's not. your WHERE clause is in the wrong spot, and you haven't used mysql_real_escape_string() on your variables. and wrap your variables in the query with curly braces '{$firstname}' if you are going to do it that way. EDIT: do you have a platform you can test these queries on? please tell me you're not just being lazy, 'cause to be testing these out will actually allow you to learn from your mistakes .. unless this is just to pass a class or something, in which case, my time is better spent elsewhere. Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951175 Share on other sites More sharing options...
Russia Posted November 4, 2009 Author Share Posted November 4, 2009 I do have cpanel. I just have no idea what to do... Lol... Sorry mate. Okay so I updated it, but where do I put the id? Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951176 Share on other sites More sharing options...
Russia Posted November 4, 2009 Author Share Posted November 4, 2009 Like this? <?php include ('inc/config.php'); $id = $rows['id']; $sql = "UPDATE `persons` WHERE id='$id' SET `FirstName` = '".$firstname."', `LastName` = '".$lastname."', `LastName` = '".$middlename."'"; ?> or <?php include ('inc/config.php'); $id = $rows['id']; $sql = "UPDATE `persons` WHERE id='$id' SET `FirstName` = '".$firstname."', `LastName` = '".$lastname."', `LastName` = '".$middlename."' WHERE id='$id'"; ?> I am getting no errors. Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951180 Share on other sites More sharing options...
mrMarcus Posted November 4, 2009 Share Posted November 4, 2009 is $id an integer or string? if it's an integer, single quotes should not be used in the query, otherwise, as seen below is fine. try this: <?php $sql = mysql_query (" UPDATE `persons` SET `FirstName` = '".$firstname."', `LastName` = '".$lastname."', `LastName` = '".$middlename."' WHERE `id` = '".$id."' "); ?> Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951186 Share on other sites More sharing options...
Russia Posted November 4, 2009 Author Share Posted November 4, 2009 Okay I am using it like this: <?php include ('inc/config.php'); $id = $rows['id']; $sql = mysql_query (" UPDATE `persons` SET `FirstName` = '".$firstname."', `LastName` = '".$lastname."', `LastName` = '".$middlename."' WHERE `id` = '".$id."' "); ?> Is that correct? Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951189 Share on other sites More sharing options...
Russia Posted November 4, 2009 Author Share Posted November 4, 2009 It is not working. Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951190 Share on other sites More sharing options...
Russia Posted November 4, 2009 Author Share Posted November 4, 2009 Bump. Anyone gonna help me? Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951195 Share on other sites More sharing options...
mrMarcus Posted November 4, 2009 Share Posted November 4, 2009 you really, really need to pay attention to everything that somebody tells you. for example, where have you set your variables $firstname, $middlename, $lastname? you're trying to jump to the conclusion so quickly that the means to get to the end just aren't lining up. i showed you to declare your variables as such: <?php $firstname = mysql_real_escape_string ($_POST['firstname']); //and so on... ?> otherwise, the query will not have anything to use. and, add: OR die (mysql_error()); to the end of the query, replacing the last semi-colon with the code above. and, are you sure you are setting $id with a value? what's going on inside config.php? Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951208 Share on other sites More sharing options...
Russia Posted November 4, 2009 Author Share Posted November 4, 2009 Okay so like this? <?php include ('inc/config.php'); $firstname = mysql_real_escape_string ($_POST['firstname']); $lastname = mysql_real_escape_string ($_POST['lastname']); $middlename= mysql_real_escape_string ($_POST['middlename']); $id = $rows['id']; $sql = mysql_query (" UPDATE `persons` SET `FirstName` = '".$firstname."', `LastName` = '".$lastname."', `LastName` = '".$middlename."' WHERE `id` = '".$id."' "); OR die (mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951212 Share on other sites More sharing options...
Russia Posted November 4, 2009 Author Share Posted November 4, 2009 <?php include ('inc/config.php'); $firstname = mysql_real_escape_string ($_POST['firstname']); $lastname = mysql_real_escape_string ($_POST['lastname']); $middlename= mysql_real_escape_string ($_POST['middlename']); $id = 2; $sql = mysql_query (" UPDATE `testing` SET `FirstName` = '".$firstname."', `LastName` = '".$lastname."', `MiddleName` = '".$middlename."' WHERE `id` = '".$id."' ") OR die (mysql_error()); ?> Fixed so there's no error Right now it is editing row 2. But in my column I only have 1 row and will have only 1 row, so how would I do it so it automatically edits the only row there do I use the * thing? Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951215 Share on other sites More sharing options...
mrMarcus Posted November 4, 2009 Share Posted November 4, 2009 is it editing row #2 or id #2? i don't understand what you asking. your query as it stands, updates the record that has an `id` of 2. you will need to explain further what you are trying to accomplish. and "the * thing" stands for "all". Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951223 Share on other sites More sharing options...
Russia Posted November 4, 2009 Author Share Posted November 4, 2009 Okay basicly I have and will only have 1 row in the table. I want the form to edit that only row without telling it what the id of the row is. Do you understand? Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951225 Share on other sites More sharing options...
mrMarcus Posted November 4, 2009 Share Posted November 4, 2009 just remove the WHERE clause. part of becoming a developer is giving in to your curiosity, meaning, exercise those "what if" questions that run through your head. "What if" i remove the WHERE clause .. what happens? so, just take out: WHERE `id` = '".$id."' from your query. NOTE: if you ever had more than one record in the db, doing the above will overwrite ALL records .. you can add: LIMIT 1 to the end of the query to ensure only one record is updated. Link to comment https://forums.phpfreaks.com/topic/180310-form-updates-table-row/#findComment-951230 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.