Jump to content

Recommended Posts

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 :D

 

<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();
?>

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

//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
}
?>

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>

<?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();

}

?>

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>

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);

just to make life simple use this  :P

 

$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);

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();
   }
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.