Jump to content

Problem updating multiple rows...


DBookatay

Recommended Posts

I found a script online to upload multiple rows at one time, but can not get the thing to work...

Anyone spot any errors?

 

<?php

$host="xxxxxxxxxxxxxxxxxxxx"; // Host name 
$username="xxxxxx"; // Mysql username 
$password="xxxxx"; // Mysql password 
$db_name="xxxxxx"; // Database name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM Inventory ORDER BY year DESC";
$result=mysql_query($sql);

// Count table rows 
$count=mysql_num_rows($result);
?>

<table>
<form name="form1" method="post" action="test.php">
<tr> 
	<td>
		<table>
			<tr>
				<td>Stock</td>
				<td>VIN</td>
				<td>Asking</td>
			</tr>
			<? while($rows=mysql_fetch_array($result)){ ?>
			<tr>
				<td><? $id[]=$rows['stock']; ?><? echo $rows['stock']; ?></td>
				<td><input size="19" name="vin[]" type="text" id="vin" value="<? echo $rows['vin']; ?>"></td>
				<td><input size="3" name="price_asking[]" type="text" id="price_asking" value="<? echo $rows['price_asking']; ?>"></td>
			</tr>
			<? } ?>
			<tr><td colspan="9" align="right"><input type="submit" name="Submit" value="Submit"></td></tr>
		</table>
	</td>
</tr>
</form>
</table>
<?php

// Check if button name "Submit" is active, do this 

if($Submit){
	for($i=0;$i<$count;$i++){

		$sql1="UPDATE Inventory SET 
			vin='$vin[$i]', 
			price_asking='$price_asking[$i]' 

		WHERE stock='$id[$i]'";

		$result1=mysql_query($sql1);
	}
}

if($result1){
	header("location:test.php");
}

mysql_close();
?>

Link to comment
https://forums.phpfreaks.com/topic/174767-problem-updating-multiple-rows/
Share on other sites

can not get the thing to work...

Yes, but what is it doing? A blank page? Just shows the form again? A php error? A mysql error?

 

There could be a couple of dozen things that could prevent it from working on any server or with any database. Unless you tell us what you see in front of you when you try it, to narrow down the problem, it is a little hard to help.

 

Also, what does adding the following two lines of code immediately after the first opening <?php tag show -

ini_set("display_errors", "1");
error_reporting(E_ALL);

I added your ini_set code and got the following errors:

 

Notice: Undefined variable: Submit in /home/.phillipina/dbookatay/login.carcityofdanbury.com/passed/New/test.php on line 49

 

Notice: Undefined variable: result1 in /home/.phillipina/dbookatay/login.carcityofdanbury.com/passed/New/test.php on line 62

The error says it all. $Submit isn't defined anywhere in your script.

 

Change it to $_POST['Submit']

 

 

I did this:

<?php

if($_POST['Submit']){
	for($i=0;$i<$count;$i++){

		$sql1="UPDATE Inventory SET 
			vin='$vin[$i]', 
			price_asking='$price_asking[$i]' 

		WHERE stock='$stock[$i]'";

		$result1=mysql_query($sql1);
	}

if($result1){header("location:test.php");}
}

mysql_close();
?>

and it deleted every row in the dB. (Good thing I had it saved)

Does anyone have a better working example than the one I found?

The example you are using relies on a very old feature/bug called register globals. You still have undefined variables.

 

Where is $price_asking, $vin  and $stock defined? You use them in your query but don't define them anywhere.

 

The data your looking to use within your query will be found within the $_POST array.

 

Instead of looking around for examples of simple code (and finding terrible examples at that) why not try learning PHP yourself? Scripts like this are really quite straight forward once you know the basics.

The example you are using relies on a very old feature/bug called register globals. You still have undefined variables.

 

Where is $price_asking, $vin  and $stock defined? You use them in your query but don't define them anywhere.

 

The data your looking to use within your query will be found within the $_POST array.

 

Instead of looking around for examples of simple code (and finding terrible examples at that) why not try learning PHP yourself? Scripts like this are really quite straight forward once you know the basics.

 

That is how I have been learning, buy looking at working examples... Do you know of a good working example?

The example you are using relies on a very old feature/bug called register globals. You still have undefined variables.

 

Where is $price_asking, $vin  and $stock defined? You use them in your query but don't define them anywhere.

 

The data your looking to use within your query will be found within the $_POST array.

 

Instead of looking around for examples of simple code (and finding terrible examples at that) why not try learning PHP yourself? Scripts like this are really quite straight forward once you know the basics.

 

That is how I have been learning, buy looking at working examples... Do you know of a good working example?

 

The problem is without knowing the language you don't know whats a good example or a bad example.

 

Learning the language itself you won't need to look for examples to be able to use a script, you'll just write one.

 

Theres a good free book in my signature (Hudzilla), you could start there.

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.