Page 1 of 1

UPDATE multiple rows? SOLVED

Posted: Thu Jan 13, 2011 4:55 pm
by jsk1gcc
hey can someone advice me on how to make my update script update multiple rows.

here's what i've got so far:
update.php

Code: Select all

<?php
require('connect.php');

$update = mysql_query("SELECT DISTINCT rideName FROM ride ORDER BY rideName DESC" );
$numrows = mysql_num_rows($update);

echo "<form action='mysql_update.php' method='POST'>
<select name='updateRide' >";


while ($row=mysql_fetch_assoc($update))
{
			#$rideID = $row ['rideID'];
			$rideName = $row ['rideName'];
			#$seatNumber = $row ['seatNumber'];
			#$time = $row ['time'];
			#$price = $row ['price'];


echo "<option value='$id'> $rideName </option>";



}
echo "</select>
<input type='text' name='tochange'> 
<input type='submit' name='submit' value='change'>      
</form>";
?>


and now the handler script: mysql_update.php

Code: Select all

<?php
require ('connect.php');

$updateRide=$_POST['updateRide'];
$tochange=$_POST['tochange'];

if ($updateRide&&$tochange)
{
$change = mysql_query("UPDATE ride SET rideName='$tochange'
WHERE $id='updateRide'");

}

?>
When I try this I get nothing, no changes. In the table there is 3 distinct rideNames each have more than 20 rows. Is there a way using the drop down box and ext field like i Have to update all of the distinct rideNames in the table?

Thanks.

Re: UPDATE multiple rows?

Posted: Thu Jan 13, 2011 5:21 pm
by danwguy
I am a little confused, Where are you getting the $id variable, and in your update script why are you saying where $id='updateRide' instead od id='UpdateRide. Is there a column in your table called id?

Re: UPDATE multiple rows?

Posted: Thu Jan 13, 2011 5:21 pm
by josh
The answer is you're passing no $id. The way you'd arrive at that answer is by looking at the mysql_error() function and also echo the query before you run it through mysql_query() and look at the SQL you're sending to the database.

Re: UPDATE multiple rows? SOLVED

Posted: Thu Jan 13, 2011 6:42 pm
by jsk1gcc
Thanks, I solved it. between you both I managed to solve the problem. I had at some point # out my rideID and didn't change the $var name.

Thanks for the help :D

code incase anyone else has the problem

update.php

Code: Select all

$update = mysql_query("SELECT DISTINCT rideID, rideName FROM ride ORDER BY rideName DESC" ) ;
$numrows = mysql_num_rows($update);

echo "<form action='mysql_update.php' method='POST'>
<select name='updateRide' >";


while ($row=mysql_fetch_array($update))
{
                        $rideID = $row ['rideID'];
                        $rideName = $row ['rideName'];
                        #$seatNumber = $row ['seatNumber'];
                        #$time = $row ['time'];
                        #$price = $row ['price'];

echo $row;
echo "<option value='$rideID'> $rideName </option>";

}
echo "</select>
<input type='text' name='tochange'> 
<input type='submit' name='submit' value='change'> 
</form>";
?>
mysql_update.php

Code: Select all

<?php
require ('connect.php');

$updateRide=$_POST['updateRide'];
$tochange=$_POST['tochange'];

if ($updateRide&&$tochange)
{
$change = mysql_query("UPDATE ride SET rideName='$tochange'
WHERE $rideID='updateRide'");

}

?>


Re: UPDATE multiple rows? SOLVED

Posted: Thu Jan 13, 2011 8:14 pm
by jsk1gcc
ha!

again...this is the solved one, I was not refeshing the page :oops:

here is the working script

Code: Select all

<?php
require("connect.php");

$extractR = mysql_query("SELECT DISTINCT rideID, rideName FROM ride ORDER BY rideID " );
$numrows = mysql_num_rows($extractR);

echo " Change the ride name: <form action='mysql_update.php' method='POST'>
<select name='ride_name' >";


while ($row=mysql_fetch_assoc($extractR))
{
                        $rideID = $row ['rideID'];
                        $rideName = $row ['rideName'];
                        #$seatNumber = $row ['seatNumber'];
                        #$time = $row ['time'];
                        #$price = $row ['price'];value='$rideID'

echo $row;
echo "<option value='$rideID'> $rideName </option>";

}
echo "</select>
<input type='text' name='tochangeRide'> 
<input type='submit' name='submit' value='change'> 
</form>";
?>

Code: Select all

<?php
require ('connect.php');

$ride_name=$_POST['ride_name'];
$tochangeRide=$_POST['tochangeRide'];

if ($ride_name&&$tochangeRide)
{
$changeR = mysql_query("UPDATE ride SET rideName='$tochangeRide'
WHERE rideID='$ride_name'");

}

echo "You have updated the ride name to $tochangeRide";
?>