Jump to content

[SOLVED] Update all in database


timmah1

Recommended Posts

I have a database that pulls info out according to sport.

It's roughly 6 items, I have form to update the name and price of the fields.

 

My problem is, it's only updating the first one, not all of them

Here is the form

<form action="" method="post" enctype="application/x-www-form-urlencoded">
<?php
$sql = "SELECT * FROM packages WHERE sport = 'nfl'";
$q = mysql_query($sql);	
?>
<label for="header">Section Active?<br />
Yes <input <?php if (!(strcmp("$a[fball_active]","1"))) {echo "checked=\"checked\"";} ?> name="fball_active" type="radio" value="1" />
No <input <?php if (!(strcmp("$a[fball_active]","0"))) {echo "checked=\"checked\"";} ?> name="fball_active" type="radio" value="0" /><br /></label>
<table width="100%" border="0" cellspacing="0" cellpadding="0">

<?php	
//$i = 1;	
while($a = mysql_fetch_assoc($q)) { 


echo "<tr>";
echo "<td><label for='name'>Package Name - $i</label>";
echo "<input type='text' class='textfield' size='50' name='name[]' value='$a[name]'> </td>";
echo "<td><label for='name'>Package Price</label>";
echo "<input type='text' class='textfield' size='15' name='price[]' value='$a[price]'></td>";
echo "<input type='hidden' name='id' value='$a[id]'><br>";
echo "</tr>";
//$i++;
}
?>
</table>

 

And here is my update code

$name = $_POST['name'];
$price = $_POST['price'];
$id = $_POST['id'];

for($i=0;$i < count($id);$i++ ){
$name = $name[$i];
$price = $price[$i];

$query= sprintf("UPDATE packages SET name='%s', price='%s' WHERE id = '$id'",
mysql_real_escape_string($name),
mysql_real_escape_string($price));
mysql_query($query) or die ("Error in query: $query");
}

 

Can anyone help me to update all fields at once?

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/
Share on other sites

Anyone have any ideas?

This is just updating the top row, and I need it do update all the rows

echo "<tr>";
echo "<td><label for='name'>Package Name - $i</label>";
echo "<input type='text' class='textfield' size='50' name='name[]' value='$a[name]'> </td>";
echo "<td><label for='name'>Package Price</label>";
echo "<input type='text' class='textfield' size='15' name='price[]' value='$a[price]'></td>";
echo "<input type='hidden' name='id[]' value='$a[id]'><br>";
echo "</tr>";

 

Update Code

for($i=0;$i < count($name);$i++ ){
$name = $_POST['name'][$i];
$price = $_POST['price'][$i];
$id = $_POST['id'][$i];

$query= ("UPDATE packages SET name='$name', price='$price' WHERE id = '$id'");
mysql_query($query) or die ("Error in query: $query");

 

Thank you in advance

array(4) { ["name"]=>  array(6) { [0]=>  string(5) "first" [1]=>  string(6) "second" [2]=>  string(5) "third" [3]=>  string(6) "fourth" [4]=>  string(5) "fifth" [5]=>  string(5) "sixth" } ["price"]=>  array(6) { [0]=>  string(11) "first_price" [1]=>  string(12) "second_price" [2]=>  string(11) "third_price" [3]=>  string(12) "fourth_price" [4]=>  string(11) "fifth_price" [5]=>  string(11) "sixth_price" } ["id"]=>  string(2) "24" ["update"]=>  string(10) "Update NFL" } 

Yes.

I noticed that also, and changed it

Here is the current one

array(4) { ["name"]=>  array(6) { [0]=>  string(6) "wwwwww" [1]=>  string(14) "Sdddddqqqqqqqq" [2]=>  string(14) "Sdddddqqqqqqqq" [3]=>  string(14) "Sdddddqqqqqqqq" [4]=>  string(14) "Sdddddqqqqqqqq" [5]=>  string(6) "wwwwww" } ["price"]=>  array(6) { [0]=>  string(2) "%s" [1]=>  string(0) "" [2]=>  string(0) "" [3]=>  string(0) "" [4]=>  string(0) "" [5]=>  string(2) "%s" } ["id"]=>  array(6) { [0]=>  string(2) "19" [1]=>  string(2) "20" [2]=>  string(2) "21" [3]=>  string(2) "22" [4]=>  string(2) "23" [5]=>  string(2) "24" } ["update"]=>  string(10) "Update NFL" } 

Is there a better to update everything than with this?

$id = $_POST['id'];
for($i=0;$i < count($id);$i++ ){
$name = $_POST['name'][$i];
$price = $_POST['price'][$i];

$sql = mysql_query("UPDATE packages SET name = '$name', price = '$price' WHERE id = '$id")
or die("Sorry, there was a problem creating your account ".mysql_error());
}

<? 
require("config.php");

if($_POST['update']) {

//var_dump($_POST); 	


for($i=0;$i < count($_POST['id']);$i++ ){
$name = $_POST['name'][$i];
$price = $_POST['price'][$i];

$sql = mysql_query("UPDATE packages SET name = '$name', price = '$price' WHERE id = '$_POST[id]'")
or die("Sorry, there was a problem creating your account ".mysql_error());
}


require("header.php");
echo "<label for='header'>NFL has been updated</span>";

}
else {
require("header.php");
?>
<label for="header">Please use this form to change any variables for the NFL<br></label>
<form action="" method="post" enctype="application/x-www-form-urlencoded">
<?php
$sql = "SELECT * FROM packages WHERE sport = 'nfl'";
$q = mysql_query($sql);	
?>
<label for="header">Section Active?<br />
Yes <input <?php if (!(strcmp("$a[fball_active]","1"))) {echo "checked=\"checked\"";} ?> name="fball_active" type="radio" value="1" />
No <input <?php if (!(strcmp("$a[fball_active]","0"))) {echo "checked=\"checked\"";} ?> name="fball_active" type="radio" value="0" /><br /></label>
<table width="100%" border="0" cellspacing="0" cellpadding="0">

<?php	
//$i = 1;	
while($a = mysql_fetch_assoc($q)) { 


echo "<tr>";
echo "<td><label for='name'>Package Name - $i</label>";
echo "<input type='text' class='textfield' size='50' name='name[]' value='$a[name]'> </td>";
echo "<td><label for='name'>Package Price</label>";
echo "<input type='text' class='textfield' size='15' name='price[]' value='$a[price]'></td>";
echo "<input type='hidden' name='id[]' value='$a[id]'><br>";
echo "</tr>";
//$i++;
}
?>
</table>
<input name="update" class="button" type="submit" value="Update NFL" />
</form>
<?php
}
require("footer.php");
?>

 

That's the update page

try this

 

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

if($_POST['update']) {

//var_dump($_POST);    


for($i=0;$i < count($_POST['id']);$i++ ){
   $name = mysql_real_escape_string($_POST['name'][$i]);
   $price = mysql_real_escape_string($_POST['price'][$i]);
   $id = (is_numeric($_POST['id'][$i]))? $_POST['id'][$i] : 0;
   
   $sql = mysql_query("UPDATE packages SET name = '$name', price = '$price' WHERE id = '$id'")
or die("Sorry, there was a problem creating your account ".mysql_error());
   }
   
   
require("header.php");
echo "<label for='header'>NFL has been updated</span>";

}
else {
require("header.php");
?>
<label for="header">Please use this form to change any variables for the NFL<br></label>
<form action="" method="post" enctype="application/x-www-form-urlencoded">
<?php
$sql = "SELECT * FROM packages WHERE sport = 'nfl'";
$q = mysql_query($sql);   
?>
<label for="header">Section Active?<br />
Yes <input <?php if (!(strcmp("$a[fball_active]","1"))) {echo "checked=\"checked\"";} ?> name="fball_active" type="radio" value="1" />
No <input <?php if (!(strcmp("$a[fball_active]","0"))) {echo "checked=\"checked\"";} ?> name="fball_active" type="radio" value="0" /><br /></label>
<table width="100%" border="0" cellspacing="0" cellpadding="0">

<?php   
//$i = 1;   
while($a = mysql_fetch_assoc($q)) { 


echo "<tr>";
echo "<td><label for='name'>Package Name - $i</label>";
echo "<input type='text' class='textfield' size='50' name='name[]' value='$a[name]'> </td>";
echo "<td><label for='name'>Package Price</label>";
echo "<input type='text' class='textfield' size='15' name='price[]' value='$a[price]'></td>";
echo "<input type='hidden' name='id[]' value='$a[id]'><br>";
echo "</tr>";
//$i++;
}
?>
</table>
<input name="update" class="button" type="submit" value="Update NFL" />
</form>
<?php
}
require("footer.php");
?>

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.