andrewtwice Posted December 31, 2007 Share Posted December 31, 2007 hi there. i can't get this to work. i want to 1)call data from mysql, 2)feed it into a form, 3)then be able to change them, 4)hit submit and the records are updated. i've got the first, second and third parts figured out but the submit button does nothing. whatever fields you changed go back to what they were and nothing was changed in the database. i've spent most of today trying to figure it out and i've decided to see if any of you can tell me what's wrong here... of course im a n00b to sql, so i'm sure you look at my code and find the answer quickly, and it'll probably be a misplaced "" or something. here's the code excerpt for the form and the submit button & its logic. thanks in advance <tr> <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td> <td align="center"><input name="adnum[]" type="text" id="adnum" value="<? echo $rows['adnum']; ?>"></td> <td align="center"><input name="acct[]" type="text" id="acct" value="<? echo $rows['acct']; ?>"></td> <td align="center"><input name="size[]" type="text" id="size" value="<? echo $rows['size']; ?>"></td> <td align="center"><input name="rundate[]" type="text" id="rundate" value="<? echo $rows['rundate']; ?>"></td> <td align="center"><input name="rep[]" type="text" id="rep" value="<? echo $rows['rep']; ?>"></td> <td align="center"><input name="comments[]" type="text" id="comments" value="<? echo $rows['comments']; ?>"></td> <td align="center"><input name="inprogress[]" type="checkbox" id="inprogress" value="<? echo $rows['inprogress']; ?>"></td> <td align="center"><input name="waitcopy[]" type="checkbox" id="waitcopy" value="<? echo $rows['waitcopy']; ?>"></td> <td align="center"><input name="outproof[]" type="checkbox" id="outproof" value="<? echo $rows['outproof']; ?>"></td> <td align="center"><input name="camready[]" type="checkbox" id="camready" value="<? echo $rows['camready']; ?>"></td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><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 $tbl_name SET adnum=' $adnum[$i]', acct=' $acct[$i]', size=' $size[$i]', rundate=' $rundate[$i]', rep=' $rep[$i]', comments=' $comments[$i]', inprogress=' $inprogress[$i]', waitcopy=' $waitcopy[$i]', outproof=' $outproof[$i]', camready=' $camready[$i]', WHERE id=$id[$i]"; $result1=mysql_query($sql1); } } if($result1){ header("location:theirs2.php"); } mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/ Share on other sites More sharing options...
tidalik Posted December 31, 2007 Share Posted December 31, 2007 You should always check if the form has been submitted FIRST, then if it has - update the database. If it hasn't, then output the form with all the data. Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426397 Share on other sites More sharing options...
andrewtwice Posted December 31, 2007 Author Share Posted December 31, 2007 eep how do i do that Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426399 Share on other sites More sharing options...
PHP_PhREEEk Posted December 31, 2007 Share Posted December 31, 2007 I wrote an entire script for another user here, which takes info from MySQL, loads it into a web form, then submits changes. You can view the code here: http://www.phpfreaks.com/forums/index.php/topic,166794.msg735733.html#msg735733 It should answer any question you have regarding updating MySQL via form elements. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426402 Share on other sites More sharing options...
tidalik Posted December 31, 2007 Share Posted December 31, 2007 //Check if the form has been submitted if ($_GET[submit]){ //update the database } else { //output the form } Fit your code parts into that framework. Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426405 Share on other sites More sharing options...
mr_mind Posted December 31, 2007 Share Posted December 31, 2007 //Check if the form has been submitted if ($_GET[submit]){ //update the database } else { //output the form } Fit your code parts into that framework. we want to POST the submit button not GET it <?php if(isset($_POST['submit'])) { // Database update code } else { // Form code } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426410 Share on other sites More sharing options...
andrewtwice Posted December 31, 2007 Author Share Posted December 31, 2007 excellent! i took a break and came back and there were already a load of replies. thanks guys!! i'll work with the code and let you know what happens shortly. Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426435 Share on other sites More sharing options...
andrewtwice Posted December 31, 2007 Author Share Posted December 31, 2007 hrm. it still isn't working... i understand what you mean about getting things in order, the script makes more sense to me and probably the parser too. the code now looks like this: *note where i commented inside about maybe printing instead of closing php tag around form element? <?php $host="localhost"; $username="root"; $password=""; $db_name="ads"; $tbl_name="rop"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); // Count table rows $count=mysql_num_rows($result); ?> <?php if(isset($_POST['submit'])) { // Database update code // Check if button name "Submit" is active, do this if($Submit){ for($i=0;$i<$count;$i++){ $sql1="UPDATE $tbl_name SET adnum=' $adnum[$i]', acct=' $acct[$i]', size=' $size[$i]', rundate=' $rundate[$i]', rep=' $rep[$i]', comments=' $comments[$i]', inprogress=' $inprogress[$i]', waitcopy=' $waitcopy[$i]', outproof=' $outproof[$i]', camready=' $camready[$i]', WHERE id=$id[$i]"; $result1=mysql_query($sql1); } } } else { // Form goes here ?> //I just closed php here because i got an error about an unexpected < //because otherwise i'd have to print it inside the phptag. how do i do that? <table width="500" border="0" cellspacing="1" cellpadding="0"> <form name="form1" method="post" action=""> <tr> <td> <table width="500" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>Ad #:</strong></td> <td align="center"><strong>Account:</strong></td> <td align="center"><strong>Size:</strong></td> <td align="center"><strong>Run Date:</strong></td> <td align="center"><strong>Rep:</strong></td> <td align="center"><strong>Comments:</strong></td> <td align="center"><strong>Pickup:</strong></td> <td align="center"><strong>Waitcopy:</strong></td> <td align="center"><strong>Out on Proof:</strong></td> <td align="center"><strong>Camera Ready:</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td> <td align="center"><input name="adnum[]" type="text" id="adnum" value="<? echo $rows['adnum']; ?>"></td> <td align="center"><input name="acct[]" type="text" id="acct" value="<? echo $rows['acct']; ?>"></td> <td align="center"><input name="size[]" type="text" id="size" value="<? echo $rows['size']; ?>"></td> <td align="center"><input name="rundate[]" type="text" id="rundate" value="<? echo $rows['rundate']; ?>"></td> <td align="center"><input name="rep[]" type="text" id="rep" value="<? echo $rows['rep']; ?>"></td> <td align="center"><input name="comments[]" type="text" id="comments" value="<? echo $rows['comments']; ?>"></td> <td align="center"><input name="inprogress[]" type="checkbox" id="inprogress" value="<? echo $rows['inprogress']; ?>"></td> <td align="center"><input name="waitcopy[]" type="checkbox" id="waitcopy" value="<? echo $rows['waitcopy']; ?>"></td> <td align="center"><input name="outproof[]" type="checkbox" id="outproof" value="<? echo $rows['outproof']; ?>"></td> <td align="center"><input name="camready[]" type="checkbox" id="camready" value="<? echo $rows['camready']; ?>"></td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </tr> </form> </table> <?php } if($result1){ header("location:theirs2.php"); } mysql_close(); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426457 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 you have specified many array in your updated statements where are they coming from ? Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426461 Share on other sites More sharing options...
mmarif4u Posted December 31, 2007 Share Posted December 31, 2007 May i know where you define this: if($Submit) I mean your submit variable: $Submit Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426462 Share on other sites More sharing options...
mr_mind Posted December 31, 2007 Share Posted December 31, 2007 <?php $host="localhost"; $username="root"; $password=""; $db_name="ads"; $tbl_name="rop"; if(mysql_connect("$host", "$username", "$password")) { if(mysql_select_db("$db_name")) { $sql = "SELECT * FROM $tbl_name"; $result = mysql_query($sql); // Count table rows $count = mysql_num_rows($result); if(isset($_POST['submit'])) { // Database update code for($i=0;$i<$count;$i++){ $sql1 = "UPDATE $tbl_name SET " $sql1 .= "adnum='" . $adnum[$i] . "', $sql1 .= "acct='" . $acct[$i] . "', $sql1 .= "size='" . $size[$i] . "', $sql1 .= "rundate='" . $rundate[$i] . "', $sql1 .= "rep='" . $rep[$i] . "', $sql1 .= "comments='" . $comments[$i] . "', $sql1 .= "inprogress='" . $inprogress[$i] . "', $sql1 .= "waitcopy='" . $waitcopy[$i] . "', $sql1 .= "outproof='" . $outproof[$i] . "', $sql1 .= "camready='" . $camready[$i] . "', $sql1 .= "WHERE id='" . $id[$i] . "'"; $result1=mysql_query($sql1); } } else { // Form goes here print '<table width="500" border="0" cellspacing="1" cellpadding="0">'; print '<form name="form1" method="post" action="' . $_SERVER['PHP_SELF'] . '">'; print '<tr>'; print '<td>'; print '<table width="500" border="0" cellspacing="1" cellpadding="0">'; print '<tr>'; print '<td align="center"><strong>Ad #:</strong></td>'; print '<td align="center"><strong>Account:</strong></td>'; print '<td align="center"><strong>Size:</strong></td>'; print '<td align="center"><strong>Run Date:</strong></td>'; print '<td align="center"><strong>Rep:</strong></td>'; print '<td align="center"><strong>Comments:</strong></td>'; print '<td align="center"><strong>Pickup:</strong></td>'; print '<td align="center"><strong>Waitcopy:</strong></td>'; print '<td align="center"><strong>Out on Proof:</strong></td>'; print '<td align="center"><strong>Camera Ready:</strong></td>'; print '</tr>'; while($rows=mysql_fetch_array($result)){ $id[]=$rows['id']; print '<tr>'; print '<td align="center">' . $rows['id'] . '</td>'; print '<td align="center"><input name="adnum[]" type="text" id="adnum" value="' . $rows['adnum'] . '"></td>'; print '<td align="center"><input name="acct[]" type="text" id="acct" value="' . $rows['acct'] . '"></td>'; print '<td align="center"><input name="size[]" type="text" id="size" value="' . $rows['size'] . '"></td>'; print '<td align="center"><input name="rundate[]" type="text" id="rundate" value="' . $rows['rundate'] . '"></td>'; print '<td align="center"><input name="rep[]" type="text" id="rep" value="' . $rows['rep'] . '"></td>'; print '<td align="center"><input name="comments[]" type="text" id="comments" value="' . $rows['comments'] . '"></td>'; print '<td align="center"><input name="inprogress[]" type="checkbox" id="inprogress" value="' . $rows['inprogress'] . '"></td>'; print '<td align="center"><input name="waitcopy[]" type="checkbox" id="waitcopy" value="' . $rows['waitcopy'] . '"></td>'; print '<td align="center"><input name="outproof[]" type="checkbox" id="outproof" value="' . $rows['outproof'] . '"></td>'; print '<td align="center"><input name="camready[]" type="checkbox" id="camready" value="' . $rows['camready'] . '"></td>'; print '</tr>'; } print '<tr>'; print '<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>'; print '</tr>'; print '</table>'; print '</td>'; print '</tr>'; print '</form>'; print '</table>'; } if($result1){ header("location:theirs2.php"); } } else { print mysql_error(); } mysql_close(); } else { print mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426465 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 this <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td> should be <td align="center"><input type="hidden" name="id[]" value=<? echo $rows['id']; ?><? echo $rows['id']; ?></td> Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426471 Share on other sites More sharing options...
andrewtwice Posted December 31, 2007 Author Share Posted December 31, 2007 wow!! thank you for all that work! ...unfortunately it says Parse error: syntax error, unexpected T_VARIABLE on line 23. which is: $sql1 .= "adnum='" . $adnum[$i] . "', Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426474 Share on other sites More sharing options...
mr_mind Posted December 31, 2007 Share Posted December 31, 2007 oh sorry add "; to the end of each line that starts with $sql1 .= Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426475 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 actually it should of been <td align="center"><input type="hidden" name="id[]" value="<? echo $rows['id']; ?>"><? echo $rows['id']; ?></td> missed out on the syntax hope you got it right Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426477 Share on other sites More sharing options...
andrewtwice Posted December 31, 2007 Author Share Posted December 31, 2007 mr_mind: will do, brb to test rajiv: i'm trying that one in the other script... thanks for the syntax Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426483 Share on other sites More sharing options...
andrewtwice Posted December 31, 2007 Author Share Posted December 31, 2007 lol now it says line 24 instead of 23... looks like this now: $sql1 = "UPDATE $tbl_name SET "; $sql1 .= "adnum='" . $adnum[$i] . "',; $sql1 .= "acct='" . $acct[$i] . "',; $sql1 .= "size='" . $size[$i] . "',; $sql1 .= "rundate='" . $rundate[$i] . "',; $sql1 .= "rep='" . $rep[$i] . "',; $sql1 .= "comments='" . $comments[$i] . "',; $sql1 .= "inprogress='" . $inprogress[$i] . "',; $sql1 .= "waitcopy='" . $waitcopy[$i] . "',; $sql1 .= "outproof='" . $outproof[$i] . "',; $sql1 .= "camready='" . $camready[$i] . "',; $sql1 .= "WHERE id='" . $id[$i] . "'"; $result1=mysql_query($sql1); Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426484 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 just to make life simple use this $sql1 = "UPDATE $tbl_name SET adnum='{$adnum[$i]}',acct='{$acct[$i]}',size='{$size[$i]}',rundate='{$rundate[$i]}', rep='{$rep[$i]}',comments='{$comments[$i]}',inprogress='{$inprogress[$i]}',waitcopy='{$waitcopy[$i]}', outproof='{$outproof[$i]}',camready='{$camready[$i]}' WHERE id='{$id[$i]}'"; $result1=mysql_query($sql1); Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426487 Share on other sites More sharing options...
andrewtwice Posted December 31, 2007 Author Share Posted December 31, 2007 cool now it isn't throwing any errors but it still won't update. wow this is becoming really stressful! ??? Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426496 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 this $result1=mysql_query($sql1); should be $result1=mysql_query($sql1) or die("cannot execute: ".mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426499 Share on other sites More sharing options...
andrewtwice Posted December 31, 2007 Author Share Posted December 31, 2007 here you can see what it's doing: if you change something then hit submit it just changes it back. Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426503 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 well there is something really wrong with the code... I try to fix it and post it... Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426508 Share on other sites More sharing options...
andrewtwice Posted December 31, 2007 Author Share Posted December 31, 2007 rajiv you are my hero. Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426510 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 try this <?php $host="localhost"; $username="root"; $password=""; $db_name="ads"; $tbl_name="rop"; if(mysql_connect("$host", "$username", "$password")) { if(mysql_select_db("$db_name")) { $count = count($_POST['id']); if(isset($_POST['Submit'])) { // Database update code for($i=0;$i<$count;$i++){ $sql1 = "UPDATE $tbl_name SET adnum='{$adnum[$i]}',acct='{$acct[$i]}',size='{$size[$i]}',rundate='{$rundate[$i]}',rep='{$rep[$i]}',comments='{$comments[$i]}',inprogress='{$inprogress[$i]}',waitcopy='{$waitcopy[$i]}', outproof='{$outproof[$i]}',camready='{$camready[$i]}' WHERE id='{$id[$i]}'"; $result1=mysql_query($sql1); } } $sql = "SELECT * FROM $tbl_name"; $result = mysql_query($sql); // Form goes here print '<table width="500" border="0" cellspacing="1" cellpadding="0">'; print '<form name="form1" method="post" action="' . $_SERVER['PHP_SELF'] . '">'; print '<tr>'; print '<td>'; print '<table width="500" border="0" cellspacing="1" cellpadding="0">'; print '<tr>'; print '<td align="center"><strong>Ad #:</strong></td>'; print '<td align="center"><strong>Account:</strong></td>'; print '<td align="center"><strong>Size:</strong></td>'; print '<td align="center"><strong>Run Date:</strong></td>'; print '<td align="center"><strong>Rep:</strong></td>'; print '<td align="center"><strong>Comments:</strong></td>'; print '<td align="center"><strong>Pickup:</strong></td>'; print '<td align="center"><strong>Waitcopy:</strong></td>'; print '<td align="center"><strong>Out on Proof:</strong></td>'; print '<td align="center"><strong>Camera Ready:</strong></td>'; print '</tr>'; while($rows=mysql_fetch_array($result)){ print '<tr>'; print '<td align="center"><input type="hidden" name="id[]" value="' . $rows['id'] . '">' . $rows['id'] . '</td>'; print '<td align="center"><input name="adnum[]" type="text" id="adnum" value="' . $rows['adnum'] . '"></td>'; print '<td align="center"><input name="acct[]" type="text" id="acct" value="' . $rows['acct'] . '"></td>'; print '<td align="center"><input name="size[]" type="text" id="size" value="' . $rows['size'] . '"></td>'; print '<td align="center"><input name="rundate[]" type="text" id="rundate" value="' . $rows['rundate'] . '"></td>'; print '<td align="center"><input name="rep[]" type="text" id="rep" value="' . $rows['rep'] . '"></td>'; print '<td align="center"><input name="comments[]" type="text" id="comments" value="' . $rows['comments'] . '"></td>'; print '<td align="center"><input name="inprogress[]" type="checkbox" id="inprogress" value="' . $rows['inprogress'] . '"></td>'; print '<td align="center"><input name="waitcopy[]" type="checkbox" id="waitcopy" value="' . $rows['waitcopy'] . '"></td>'; print '<td align="center"><input name="outproof[]" type="checkbox" id="outproof" value="' . $rows['outproof'] . '"></td>'; print '<td align="center"><input name="camready[]" type="checkbox" id="camready" value="' . $rows['camready'] . '"></td>'; print '</tr>'; } print '<tr>'; print '<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>'; print '</tr>'; print '</table>'; print '</td>'; print '</tr>'; print '</form>'; print '</table>'; if($result1){ header("location:theirs2.php"); } } else { print mysql_error(); } mysql_close(); } else { print mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426535 Share on other sites More sharing options...
andrewtwice Posted December 31, 2007 Author Share Posted December 31, 2007 k trying... Quote Link to comment https://forums.phpfreaks.com/topic/83802-solved-oh-noes-my-update-button-does-nothing/#findComment-426536 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.