Thunder_Wolf Posted October 7, 2014 Share Posted October 7, 2014 I have created a form that i can see the form input info in to the form boxes but once submitted it is not updating or adding new input to the database. I am trying to keep it as simple as possible. I am also new at PHP. Here is my code: <?php // define variables and set to empty values $amp_20_parts_idErr = $part_numberErr = $locationErr = $quantityErr = ""; $amp_20_parts_id = $part_number = $discription = $location = $quantity = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["amp_20_parts_id"])) { $amp_20_parts_idErr = "ID is required."; } else { $amp_20_parts_id= test_input($_POST["amp_20_parts_id"]); } if (empty($_POST["part_number"])) { $part_numberErr = "Part number is required."; } else { $email = test_input($_POST["part_number"]); } if (empty($_POST["description"])) { $descriptionErr = ""; } else { $description = test_input($_POST["description"]); } if (empty($_POST["location"])) { $locationErr = "A location is required."; } else { $location = test_input($_POST["location"]); } if (empty($_POST["quantity"])) { $quantityErr = "Quantity is required"; } else { $quantity = test_input($_POST["quantity"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <div id="update_form"> Please follow instructions for updating the data base.<br>instructional text goes here.<br/><br/> <table><tr><form method="POST" action="new path"> <td>ID: <input name="amp_20_parts_id" type="text"><span><?php echo $amp_20_parts_idErr; ?></span><br/><br/></td> <td>Part Number: <input name="part_number" type="text"><span><?php echo $part_numberErr; ?></span><br/><br/></td> <td><label>Discription: <textarea name="description" rows="3" col="20"></textarea> <br/><br/></td> <td>Location: <input name="location" type="text"><span><?php echo $locationErr;?> </span><br/><br/></td> <td>Quantity: <input name="quantity" type="text"><span><?php echo $quantityErr; ?> </span><br/><br/></td><br/> <td><input type="submit"></td> </form></tr></table> <?php $con=mysqli_connect("server","user","password","db"); // Check connection if (mysqli_connect_errno()) { echo("Connect failed: %s\n", mysqli_connect_error()); // escape variables for security $amp_20_parts_id = mysqli_real_escape_string($con, $_POST['amp_20_parts_id']); $part_number = mysqli_real_escape_string($con, $_POST['part_number']); $discription = mysqli_real_escape_string($con, $_POST['description']); $location = mysqli_real_escape_string($con, $_POST['location']); $quantity = mysqli_real_escape_string($con, $_POST['quantity']); $sql="INSERT INTO amp_20 (amp_20_parts_id, part_number, description, location, quantity) VALUES ('$amp_20_parts_id', '$part_number', '$description', '$location', '$quantity')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/ Share on other sites More sharing options...
ginerjm Posted October 7, 2014 Share Posted October 7, 2014 You seem to be rusty on html as well. Your input tags have no value= clause. PS - imho - shorter var names are better. Who needs to be typing 10 or 20 char var names again and again? Too prone to error. Plus - as a newbie you would be smart to turn on php error checking - it will come in handy when you do misspell one of the var names. Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1492970 Share on other sites More sharing options...
QuickOldCar Posted October 8, 2014 Share Posted October 8, 2014 (edited) You have a typo $amp_20_parts_id = $part_number = $discription = $location = $quantity = "";//<- daisy chaining all to blank huh $discription = mysqli_real_escape_string($con, $_POST['description']); $sql="INSERT INTO amp_20 (amp_20_parts_id, part_number, description, location, quantity) VALUES ('$amp_20_parts_id', '$part_number', '$description', '$location', '$quantity')"; Edited October 8, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1492997 Share on other sites More sharing options...
Thunder_Wolf Posted October 8, 2014 Author Share Posted October 8, 2014 (edited) I did catch the typo and have corrected it, I was thinking about shortining the amp_20_ var in the script and the data base, which I have done now as well as shorting the part num var as well, will test and see if it changes the input to where it will add to the data base. Edited October 8, 2014 by Thunder_Wolf Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493025 Share on other sites More sharing options...
Thunder_Wolf Posted October 8, 2014 Author Share Posted October 8, 2014 Ginerjm, could you give an example of the value clause, I think I know what i am missing but not sure, it would help out a lot thanks Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493028 Share on other sites More sharing options...
Thunder_Wolf Posted October 8, 2014 Author Share Posted October 8, 2014 (edited) I have now after looking at it Ginerjm, i was missing the value in the html part and that was corrected and still no change still not inputing data to database and creating a new row Edited October 8, 2014 by Thunder_Wolf Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493037 Share on other sites More sharing options...
ginerjm Posted October 8, 2014 Share Posted October 8, 2014 Are you getting any errors? (ie, do you have php error checking on?) Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493038 Share on other sites More sharing options...
Thunder_Wolf Posted October 8, 2014 Author Share Posted October 8, 2014 yes and when I do get an error it gives me line and what is expected, also the editor I am using also read flag bad syntax, and no errors what so ever has shown at this time. Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493042 Share on other sites More sharing options...
ginerjm Posted October 8, 2014 Share Posted October 8, 2014 So - since you aren't showing us the messages, did you fix all the errors? Be sure to check the resuls of your query call and output the MySQL_error code if there is one. Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493048 Share on other sites More sharing options...
Thunder_Wolf Posted October 8, 2014 Author Share Posted October 8, 2014 there is no messages at all it like the inputed data is getting lost, as for the data being pulled from the database there are no issues and its pulling 100% correct, it just not adding new data from the input form. Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493052 Share on other sites More sharing options...
ginerjm Posted October 8, 2014 Share Posted October 8, 2014 Post (Properly please!) the code that is the form and then the code that processes the form values and creates the query to insert them. Be sure that you show the part where you turn on php error checking. Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493058 Share on other sites More sharing options...
Thunder_Wolf Posted October 8, 2014 Author Share Posted October 8, 2014 (edited) <?php error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); // define variables and set to empty values $amp20ptidErr = $partnumErr = $locationErr = $quantityErr = ""; $amp20ptid = $partnum = $description = $location = $quantity = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["amp20ptid"])) { $amp20ptidErr = "ID is required."; } else { $amp20ptid= test_input($_POST["amp20ptid"]); } if (empty($_POST["partnum"])) { $partnumErr = "Part number is required."; } else { $partnum = test_input($_POST["partnum"]); } if (empty($_POST["description"])) { $descriptionErr = ""; } else { $description = test_input($_POST["description"]); } if (empty($_POST["location"])) { $locationErr = "A location is required."; } else { $location = test_input($_POST["location"]); } if (empty($_POST["quantity"])) { $quantityErr = "Quantity is required"; } else { $quantity = test_input($_POST["quantity"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <div id="update_form"> Please follow instructions for updating the data base.<br> 1 - Please use the part number for the ID ie. part number is 20-1234, remove the hypen, so ID would be 201234, if it has been duplacted, message will show, <br>if this is the case add a 1 or next avalible number of greater value, ID can be no longer then 11 numbers.<br/>2 - Must put ONLY numbers in the Part ID section, please use numeric values for letters, IE. a=1, b=2, c=3, etc. No hyphens, commas or period can be used!<br/> 3- All field MUST be filled out or you will get an error. The description can be lefts without any infomation in it but it is advised to enter a description before submiting.<br> <table><tr><form method="POST" action="addnewpart.php"> <td>ID: <input name="amp20ptid" type="text" value = ""><span><?php echo $amp20ptidErr; ?></span><br/><br/></td> <td>Part Number: <input name="partnum" type="text" value = ""><span><?php echo $partnumErr; ?></span><br/><br/></td> <td><label>Description: <textarea name="description" rows="1" col="1" ></textarea><br/><br/></td> <td>Location: <input name="location" type="text" value = ""><span><?php echo $locationErr; ?></span><br/><br/></td> <td>Quantity: <input name="quantity" type="text" value = ""><span><?php echo $quantityErr; ?></span><br/><br/></td><br/> <td><input type="submit" value="Submit form"></td> </form></tr></table> <?php error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $con=mysqli_connect("localhost","scottk","ct@1446SA","part_inventory"); // Check connection if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); // escape variables for security $amp20ptid = mysqli_real_escape_string($con, $_POST['amp20ptid']); $partnum = mysqli_real_escape_string($con, $_POST['partnum']); $discription = mysqli_real_escape_string($con, $_POST['description']); $location = mysqli_real_escape_string($con, $_POST['location']); $quantity = mysqli_real_escape_string($con, $_POST['quantity']); $sql = "INSERT INTO amp_20 (amp20ptid, partnum, description, location, quantity) VALUES ('$amp20ptid', '$partnum', '$description', '$location', '$quantity')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } mysqli_close($con); } ?> Edited October 8, 2014 by mac_gyver code tags around code please Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493066 Share on other sites More sharing options...
Thunder_Wolf Posted October 8, 2014 Author Share Posted October 8, 2014 (edited) now my Err now are not working, but where it was yeasterday Edited October 8, 2014 by Thunder_Wolf Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493067 Share on other sites More sharing options...
Thunder_Wolf Posted October 8, 2014 Author Share Posted October 8, 2014 Ok fixed my Err issue Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493077 Share on other sites More sharing options...
ginerjm Posted October 8, 2014 Share Posted October 8, 2014 Seems like I've seen this code somewhere before. If this is THE ACTUAL CODE YOU RAN you should be getting an error because you misspelled your description. I don't think this is the code you ran Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493080 Share on other sites More sharing options...
Thunder_Wolf Posted October 8, 2014 Author Share Posted October 8, 2014 Yes it is what i first typed, the description typo/misspelling was fixed but still not getting errors nor is it adding to the database like it should, i am feeling like its somethiung i am missing or something simple, but i will double check all the spelling and for typos Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493081 Share on other sites More sharing options...
ginerjm Posted October 8, 2014 Share Posted October 8, 2014 Excuse me? You re-posted the same broken code DELIBERATELY when I asked you for specific code to help you out? Good by. Not wasting my time on you. Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493082 Share on other sites More sharing options...
Thunder_Wolf Posted October 8, 2014 Author Share Posted October 8, 2014 (edited) this is the only code i have to input the data to my DB and all the code i pasted is the html inputs and the PHP coding there is nothing more that handles the inputs, but here is just the php script : <?php error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $con=mysqli_connect("localhost","scottk","ct@1446SA","part_inventory"); // Check connectionif (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); // escape variables for security$amp20ptid = mysqli_real_escape_string($con, $_POST['amp20ptid']);$partnum = mysqli_real_escape_string($con, $_POST['partnum']);$discription = mysqli_real_escape_string($con, $_POST['description']);$location = mysqli_real_escape_string($con, $_POST['location']);$quantity = mysqli_real_escape_string($con, $_POST['quantity']);$sql = "INSERT INTO amp_20 (amp20ptid, partnum, description, location, quantity)VALUES ('$amp20ptid', '$partnum', '$description', '$location', '$quantity')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con));} mysqli_close($con);}?> Edited October 8, 2014 by Thunder_Wolf Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493085 Share on other sites More sharing options...
mac_gyver Posted October 8, 2014 Share Posted October 8, 2014 Please use the forum's bbcode tags (the edit form's <> button) around code when posting it in the forum. I edited your previous post, but i see that you have posted yet more code. please edit your post above. Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493087 Share on other sites More sharing options...
mac_gyver Posted October 8, 2014 Share Posted October 8, 2014 your form processing code has a misplaced } at the end that's part of the database connection error logic. none of your database INSERT code is running, because it's inside the error handling conditional statement for your database connection. the INSERT code should be inside the form processing logic, where you have tested if the form was submitted and validated the data. Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493088 Share on other sites More sharing options...
Thunder_Wolf Posted October 9, 2014 Author Share Posted October 9, 2014 I do appalogize to all for wastinbg your time, with my poor exuse for scripting as i states i am new at php, but after seeing that I could add a hardcodrd values to my data base and my form inputs would not work no matter what I did, I have even tried a rescripted version still did not work, I do want to thank all that tried to help, wont be bothering you all with my scripts thank you Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493151 Share on other sites More sharing options...
ginerjm Posted October 9, 2014 Share Posted October 9, 2014 If you gave us the scripts that you actually run we would still be willing to help. You just can't mislead us. You have to realize we are trying to help but we are blind here if we can't see your code - good or bad. Help us to help you by providing the MEANINGFUL code that RELATES to your problem next time. Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493152 Share on other sites More sharing options...
Thunder_Wolf Posted October 9, 2014 Author Share Posted October 9, 2014 (edited) the thing is we i have giving you the form code as well as the php code and there is nothing more I can give you but what I am running, I just dont know what else to give you, I have the php the pulls from my data base without issue working, I have even taken out the php code out of the main page just to see if that was causing my issues, and i was then starting to see error once that was cleaned up and it was saying "Entered data successfully" it stilll was not updating the database, I will put the php code in was I have now. but this will be what is showing "Entered data successfully". If you need my form code please let me know: <?php $con = mysqli_connect("localhost","user","password","part_inventory"); // Check connectionif (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit();} else { $result = mysqli_query($con, "SELECT * FROM amp20"); // escape variables for security$amp20ptid = mysqli_real_escape_string($con, $_POST['amp20ptid']);$partnum = mysqli_real_escape_string($con, $_POST['partnum']);$location = mysqli_real_escape_string($con, $_POST['location']);$quantity = mysqli_real_escape_string($con, $_POST['quantity']);$sql = "INSERT INTO 'part_inventory' . 'amp_20' ('amp20ptid', 'partnum', 'description', 'location', 'quantity')VALUES('$amp20ptid', '$partnum', $_POST[description], '$location', '$quantity');";if(! $result){ die('Could not enter data: ' . mysql_error()); } echo "Entered data successfully\n"; mysqli_close($con);}?> Edited October 9, 2014 by Thunder_Wolf Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493163 Share on other sites More sharing options...
Solution Thunder_Wolf Posted October 9, 2014 Author Solution Share Posted October 9, 2014 (edited) I just got it fixed: Old code - die('Could not enter data: ' . mysql_error()); new code - die('Could not enter data: ' . mysql_error($con)); then change : old code - $sql = "INSERT INTO 'part_inventory' . 'amp_20' ('amp20ptid', 'partnum', 'description', 'location', 'quantity') to new code - $sql = "INSERT INTO amp20 (amp20ptid, partnum, description, location, quantity) i was able to put 2 new entries in to my data base. Edited October 9, 2014 by Thunder_Wolf Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493173 Share on other sites More sharing options...
QuickOldCar Posted October 9, 2014 Share Posted October 9, 2014 Maybe you didn't type your fixed code correctly. mysql_* functions and mysqli_* functions do not mix. You show the fixed as die('Could not enter data: ' . mysql_error($con)); Quote Link to comment https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/#findComment-1493187 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.