PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Oct 16, 2017 11:01 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Jul 27, 2017 12:39 am 
Offline
Forum Newbie

Joined: Thu Jul 27, 2017 12:02 am
Posts: 2
I have this code I have put together. It retrieves records and shows them in the table just fine. I need to be able to change the info in the table and update the DB. I found this code for updating multiple rows in a tutorial. I cannot get it to work. Here is the code in its entirety:
Syntax: [ Download ] [ Hide ]
<!DOCTYPE html>
  <html>
  <head>
  <Title>Edit Template</title>
  </head>
  <body>
  <p>CQ Simple Phone Provisioning System -- Template Edit</p>
  <hr>
  <form action="" method="post">
 Template to edit: <input type="text" name="tname"><br>
 </form>
 <tr>
 <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
 </tr>
 </table>
 </td>
 </tr>
 </form>
 </table>
 <?php
 require('/var/www/html/cqadmin/utils/connect.php');
 $tempname = $_POST['tname'];
 print ("Editing Template: $tempname");
 
 $link = mysql_connect("localhost", "root", "XXXX") or die ('Error connecting to mysql' . mysql_error());
 mysql_select_db("cqadmin");
 
 $sql2 = "SELECT id,templatename,keyname,keyid,keytype,keyvalue,keylabel from cqadmin.keys where templatename = '$tempname';";
 $result2 = mysql_query($sql2) or die(mysql_error());
 echo "<table border='5'>
     <tr>
         <th>Keys</th>
         <th>Type</th>
        <th>Templatename</th>
        <th>Key ID</th>
        <th>Key type id</th>
        <th>Key value</th>
         <th>Key label</th>
        <th>Id</th>
     </tr>"
;
 
 while($row = mysql_fetch_assoc($result2))
 {
     $sql = "SELECT id , keyname FROM keytypes;";
     $result = mysql_query($sql) or die(mysql_error());
     echo "<td><input type='text' name='keyname[]' value='" . $row['keyname'] . "' readonly>       </td>";
     echo "<td> <select name='keyname[]'>";
     while($rowA = mysql_fetch_assoc($result)) {
         echo '<option value="' . $rowA['keyname'] . '">' . $rowA['keyname'] . '</option>';
   }
 echo "<td><input type='text' name='templatename[]' value='" . $row['templatename'] . "'></td>";
 echo "<td><input type='text' name='keyid[]' value='" . $row['keyid'] . "'></td>";
 echo "<td><input type='text' name='keytype[]' value='" . $row['keytype'] . "'></td>";
 echo "<td><input type='text' name='keyvalue[]' value='" . $row['keyvalue'] . "'></td>";
 echo "<td><input type='text' name='keylabel[]' value='" . $row['keylabel'] . "'></td>";
 echo "<td><input type='text' name='id[]' value='" . $row['id'] . "'></td>";
 echo"</tr>";
   
 }
 echo "</table>";
 // Count table rows
 $count=mysql_num_rows($result2);
 echo $count;
 
 // Check if button name "Submit" is active, do this
 if($Submit){
 for($i=0;$i<$count;$i++){
 $sql3="UPDATE keys SET templatename='$templatename[$i]', keyname='$keyname[$i]', keyid='$keyid[$i],'keytype='$keytype[$i]',keyvalue='$keyvalue[$i]',keylabel='$keylabel[$i]' WHERE id='$id[$i]'";
 $result3=mysql_query($sql3);
 }
 }
 if($result1){
 header("location:droptest.php");
 }
 mysql_close();
 ?>
 

The last bit for the Submit button is the code I got from the tutorial. When I hit the submit I get an error saying there is an unexpected > in line 57. I can't see the difference in that line and the ones above it.

I really appreciate any ideas that you guys can offer. I know the code is messy. I am just a hack here so far.

Thanks again


Top
 Profile  
 
PostPosted: Tue Aug 15, 2017 9:45 am 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 338
https://gist.github.com/thinsoldier/b54 ... le1-php-L1

You started a form on line 9 and ended it on line 11.

There's nothing in that form that will submit that form.

You started a table row on line 12 without actuall starting a table before that.

On line 13 you have a submit button that is not within a <form>...</form>

On lines 14 - 19 you are closing tags that you never opened.

Line 25 - Learn mysqli or PDO as soon as possible. mysql will not be available in PHP version 8.

I don't see why it says you have a problem on line 57. Try putting the string on the same line next to the echo and hope for the best.

I strongly advise you to do as much of you php work as possible prior to mixing it in with echoing html.

Also don't do multiple layers of nested html mixed with php code all at once.

Lines 42 - 58 I would have generated an array of <input> strings and later looped over that array to generated the <td>'s. Makes it easier to change the <td> to <div> for example if such a change was desired later.

Learn to use printf and sprintf. In many cases it's much easier to avoid syntax errors compared to glueing strings together and having to keep track of single quotes versus double quotes.

echo "<td><input type='text' name='templatename[]' value='" . $row['templatename'] . "'></td>";
etc...
becomes

Syntax: [ Download ] [ Hide ]
<?php

$inputTag = '<input type="text" name="%s[]" value="%s">';

$inputs = array();
$inputs[] = sprintf( $inputTag , 'templatename', $row['templatename']);
$inputs[] = sprintf( $inputTag , 'keyid',        $row['keyid']);
$inputs[] = sprintf( $inputTag , 'keytype',      $row['keytype']);
$inputs[] = sprintf( $inputTag , 'keyvalue',     $row['keyvalue']);
$inputs[] = sprintf( $inputTag , 'keylabel',     $row['keylabel']);

$cellTag  = "<td>%s</td>";

foreach( $inputs as $input )
{
        printf( $cellTag , $input );
}
 



If you happen to want a field for EVERY entry in $row you could do...
Syntax: [ Download ] [ Hide ]
<?php

$inputTag = '<input type="text" name="%s[]" value="%s">';

foreach( $row as $key => $value )
{
        printf( "<td>%s</td>", sprintf( $inputTag , $key , $value ) );
}

?>

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 18 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group