timmah1 Posted December 12, 2008 Share Posted December 12, 2008 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 More sharing options...
timmah1 Posted December 12, 2008 Author Share Posted December 12, 2008 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 Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713845 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 print_f("<pre>$_POST</pre>"); die(); put that at top top of the page and see what get printed out after submitting the form Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713852 Share on other sites More sharing options...
timmah1 Posted December 12, 2008 Author Share Posted December 12, 2008 Fatal error: Call to undefined function print_f() Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713853 Share on other sites More sharing options...
timmah1 Posted December 12, 2008 Author Share Posted December 12, 2008 using this print_r("<pre>$_POST</pre>"); die(); It prints out Array Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713855 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 my bad, go with this var_dump($_POST); Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713859 Share on other sites More sharing options...
timmah1 Posted December 12, 2008 Author Share Posted December 12, 2008 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" } Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713861 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 The id field in your form is currently not coming through as an array is it set to name="id[]" ? Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713865 Share on other sites More sharing options...
timmah1 Posted December 12, 2008 Author Share Posted December 12, 2008 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" } Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713868 Share on other sites More sharing options...
timmah1 Posted December 12, 2008 Author Share Posted December 12, 2008 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()); } Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713870 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 That's a pretty standard way of doing it you might want to use mysql_real_escape_string() for security, but the functionality is fine. does it work now? Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713878 Share on other sites More sharing options...
timmah1 Posted December 12, 2008 Author Share Posted December 12, 2008 no, it don't work at all Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713884 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 Ok, put paste all of your new script in code tags Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713892 Share on other sites More sharing options...
timmah1 Posted December 12, 2008 Author Share Posted December 12, 2008 <? 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 Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713893 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 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"); ?> Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713900 Share on other sites More sharing options...
timmah1 Posted December 12, 2008 Author Share Posted December 12, 2008 That works perfect now! Thank you so much gevans Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713905 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 no worries, hit resolved Link to comment https://forums.phpfreaks.com/topic/136695-solved-update-all-in-database/#findComment-713906 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.