Jump to content

[SOLVED] Last field not being written to db


sanderphp

Recommended Posts

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>

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.

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']);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.