Jump to content

Problem with MySql update


cory011202

Recommended Posts

Hello You have helped me out before lets hope we can keep the streak alive!

My problem is I am working on a page for a racer that wants to be able to update his schedule of races and have it display on his web page. I have the database queries working ( Still need to work on the date) and I can post,update and retrieve information with the db. My problem is it will only read information from the very last record pulled from the db. So if I have 5 records and I edit 1 -4 nothing will happen. if I edit 5 then I will get that information posted. if I edit 1-4 record 5 is still posted. Now if I have the radio button check on 1-4 then it wll update with the same info as the 5th record has. I am guessing I am not calling the record id or something as id is the primary key. thanks for your help.

 

 

<!--table to display and edit schedule information-->

 

<table id="schedtable" align="center">

<tr>

<td width="50">Edit</td

<td width="250">Date</td>

<td width="250">Track</td>

<td width="250">Information</td>

<td width="250">Race Results</td>

</tr>

 

<!--start form-->

 

<form method="post" action="schedule_update.php">

 

<?php

// loop to display info from db

 

while ( $row = mysql_fetch_array($cur_sched)) {

echo "<tr>";

echo  "<td width='50'>" . "<input name='id'type='radio' value=" . $row['id'] . " >" . "</td>";

echo "<td width='250'>" . "<input name='date' type='text' value=" . $row['date'] . ">" . "</td>";

echo "<td width='250'>" . "<input name='track' type='text' value=" . $row['track'] . ">" . "</td>";

echo "<td width='250'>" . "<input name='information' type='text' value=" . $row['other'] . ">" . "</td>";

echo "<td width='250'>" . "<input name='results' type='text' value=" . $row['results'] . ">" . "</td>";

echo "</tr>";

 

//end loop

}

?>

 

<!--buttons for form-->

 

<tr>

<td colspan="5" align="center">

<INPUT type="submit" value="Update" name="submit" />

</td>

</tr>

 

<!--end form-->

 

</form>

</table>

 

This is the php file for the query

 

<?php

//connect to the db

require_once('includes/connection.php');

 

//Gets the Schedule from the schedule page

$id = $_POST['id'];

$date = $_POST['date'];

$track = $_POST['track'];

$information = $_POST['information'];

$results = $_POST['results'];

 

//Query to insert data into database

$result = mysql_query("UPDATE raceinfo SET date='$date', track='$track', other='$information', results='$results'  WHERE id='$id' ")

or die(mysql_error()); 

 

//here for troubleshooting

echo $id;

echo $date;

echo $track;

echo $information;

echo $results;

 

 

//redirect after query

 

header("Location:schedule.php");

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/154356-problem-with-mysql-update/
Share on other sites

Try this

 

Your form

<table id="schedtable" align="center">
   <tr>
      <td width="50">Edit</td
      <td width="250">Date</td>
      <td width="250">Track</td>
      <td width="250">Information</td>
      <td width="250">Race Results</td>
   </tr>
   
<!--start form-->

<form method="post" action="schedule_update.php">

<?php
// loop to display info from db

   while ( $row = mysql_fetch_array($cur_sched)) {   
   echo "<tr>";
   echo   "<td width='50'>" . "<input name='id[]' type='radio' value=" . $row['id'] . " >" . "</td>";
   echo   "<td width='250'>" . "<input name='date[]' type='text' value=" . $row['date'] . ">" . "</td>";
   echo   "<td width='250'>" . "<input name='track[]' type='text' value=" . $row['track'] . ">" . "</td>";
   echo   "<td width='250'>" . "<input name='information[]' type='text' value=" . $row['other'] . ">" . "</td>";
   echo   "<td width='250'>" . "<input name='results[]' type='text' value=" . $row['results'] . ">" . "</td>";
   echo "</tr>";
   
//end loop   
}
?>

<!--buttons for form-->

   <tr>
      <td colspan="5" align="center">
         <INPUT type="submit" value="Update" name="submit" />
      </td>
   </tr>
   
<!--end form-->

   </form>
</table>

 

schedule_update.php

<?php
//connect to the db
   require_once('includes/connection.php');
   
//Gets the Schedule from the schedule page
   $id = (is_numeric($_POST['id'][$i]))? $_POST['id'][$i] : 0;
   $date = mysql_real_escape_string($_POST['date'][$i]);
   $track = mysql_real_escape_string($_POST['track'][$i]);
   $information = mysql_real_escape_string($_POST['information'][$i]);
   $results = mysql_real_escape_string($_POST['results'][$i]);

//Query to insert data into database
   $result = mysql_query("UPDATE raceinfo SET date='$date', track='$track', other='$information', results='$results'  WHERE id='$id' ")
   or die(mysql_error()); 

//here for troubleshooting
echo $id;
echo $date;
echo $track;
echo $information;
echo $results;


//redirect after query

   header("Location:schedule.php");

?>

Thanks for the quick response!

 

 

however it still only takes the last input and now it will only display the first letter of whatever I type in.

 

I dont know know why its only taking the last row and using that to update the other records. I am guessing since it will update the other records then the id is working correctly

<?php
   $id = (is_numeric($_POST['id'][$i]))? $_POST['id'][$i] : 0;
   $date = mysql_real_escape_string($_POST['date']);
   $track = mysql_real_escape_string($_POST['track']);
   $information = mysql_real_escape_string($_POST['information']);
   $results = mysql_real_escape_string($_POST['results']);
?>

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.