sanderphp Posted May 5, 2008 Share Posted May 5, 2008 I have a simple form script that has 4 fields. I'm using an echo on the page that posts the data to the db and it is showing the value correctly, but when I look at the db the field is a zero. Could my problem be that my "submit" is right below ave_speed field which is the one that is getting zeros in the db. if ($_POST['Status'] == '1') { $date = $_POST['date']; $total_distance = $_POST['total_distance']; $total_time = $_POST['total_time']; $ave_speed = $_POST['ave_speed']; $rider_id = $_SESSION['login_id']; // Insert data $query = "INSERT INTO info (`date`, `total_time`, `total_distance`,`ave_speed`,`rider_id`) VALUES ('$date', '$total_time', '$total_distance', '$ave_speed', '$rider_id')"; mysql_query($query) or die(mysql_error()); <form action="add_data.php" ... method="post"> <input type="hidden" name="Status" value="1"> <fieldset> <legend>Add Miles</legend> <label for="Date"> Date </label> <input type="text" id="date" name="date" /> <label for="total_distance"> Total Distance:</label> <input type="text" id="total_distance" name="total_distance" /> <label for="Total Time"> Total Time </label> <input type="text" id="total_time" name="total_time" /> <label for="Average Speed"> Average Speed </label> <input type="text" id="ave_speed" name="ave_speed" /> <button type="submit">Submit</button> </fieldset> </form> Link to comment https://forums.phpfreaks.com/topic/104141-solved-last-field-not-being-written-to-db/ Share on other sites More sharing options...
Fadion Posted May 5, 2008 Share Posted May 5, 2008 The code looks fine. What i can think (hardly assuming) is the data type uve used in the db. If you have set a the data type of ave_speed to int, you cant assign a string to it. Some things not related to your problem. First u dont need to have a hidden field to check if post data are submitted. Just use isset() on any of your post data, ie: if(isset($_POST['date'])){. Second u dont need an apostrophe (`) for tables and fields in your query. INSERT INTO info (date, total_time, ...) works just fine. Link to comment https://forums.phpfreaks.com/topic/104141-solved-last-field-not-being-written-to-db/#findComment-533188 Share on other sites More sharing options...
sanderphp Posted May 5, 2008 Author Share Posted May 5, 2008 I have it set to INT. Time and distance work. Thanks for the pointers on the other stuff. Link to comment https://forums.phpfreaks.com/topic/104141-solved-last-field-not-being-written-to-db/#findComment-533194 Share on other sites More sharing options...
Fadion Posted May 5, 2008 Share Posted May 5, 2008 Even if u input a number, post data will return them as strings. Eventhough the string will be converted to the data field type, so if u input '4fff' it will be inserted as '4'. Anyway just to be sure try: $ave_speed = intval($_POST['ave_speed']); Link to comment https://forums.phpfreaks.com/topic/104141-solved-last-field-not-being-written-to-db/#findComment-533205 Share on other sites More sharing options...
sanderphp Posted May 5, 2008 Author Share Posted May 5, 2008 Still didn't write that field. Now that I'm thinking about it, I did add the ave_speed field to the table after initially creating the table when I created the others. Is there any way to look to see if there is any paramater that is different. Link to comment https://forums.phpfreaks.com/topic/104141-solved-last-field-not-being-written-to-db/#findComment-533211 Share on other sites More sharing options...
Fadion Posted May 5, 2008 Share Posted May 5, 2008 If uve not messed it up and changed only the data type, there shouldnt be any problems. Try troubleshooting it. Run an insert query in phpmyadmin, run a query with a static variable (meaning not post) and see in each case it works correctly. Link to comment https://forums.phpfreaks.com/topic/104141-solved-last-field-not-being-written-to-db/#findComment-533213 Share on other sites More sharing options...
sanderphp Posted May 5, 2008 Author Share Posted May 5, 2008 "insert into info (ave_speed) Values (23)" worked and hardcoding a value in the sql query worked. Does that mean it's not being read from the from? My echo statements work. Link to comment https://forums.phpfreaks.com/topic/104141-solved-last-field-not-being-written-to-db/#findComment-533236 Share on other sites More sharing options...
sanderphp Posted May 5, 2008 Author Share Posted May 5, 2008 After using the sql manual insert and the hard code value. It's now working. Link to comment https://forums.phpfreaks.com/topic/104141-solved-last-field-not-being-written-to-db/#findComment-533246 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.