Hello,

I am getting this error when trying to run this code:

PHP Parse error: syntax error, unexpected ';' in /home/wojtek/Desktop/work/delete1.php on line 59

Here is the code:

<!-- Delete Customer Page -->

<HTML>
<HEAD> <TITLE> Delete Customer Page> </TITLE> </HEAD>
<BODY>

<FORM action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Deleting Customer Information Form.<br />
Select all customers you want to delete from the database:
<TABLE width="70%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td width=5%></td>
<td><strong>ID</strong></td>
<td><strong>Customer</strong></td>
<td><strong>Contact</strong></td>
<td><strong>Phone</strong></td>
<td><strong>Alerts</strong></td>
<td><strong>Reports</strong></td>
<td><strong>Date</strong></td>
</tr>

<?php
mysql_connect("localhost", "analyst", "voyant");
mysql_select_db("voyant");
$query = "SELECT cust_id, cust_name, contact, phone, alert_email, report_email, date FROM customer ORDER BY cust_id";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result))
{
?>
<tr>
<td><input type="checkbox" name="<?php echo $row[cust_id]; ?>" value=""></td>
<td><?php echo $row[cust_name]; ?></td>
<td><?php echo $row[contact]; ?></td>
<td><?php echo $row[phone]; ?></td>
<td><?php echo $row[alert_email]; ?></td>
<td><?php echo $row[report_email]; ?></td>
<td><?php echo $row[date]; ?></td>
</tr>
<?php } ?>
</TABLE>

<input type="submit" name"submit" value="Delete Selected">
</FORM>
<?php

//Connect to the server and select the database

mysql_connect("localhost", "analyst", "voyant");
mysql_select_db("voyant");

//Checking if the form has been submited

if (isset($POST['submit']))
{
//Loop through each customer with selected checkbox
foreach($count=0; $count < count($POST['cust_id']); $count++)
{
$cust_id = $
POST['cust_id'][$count];
$query = "DELETE FROM customer WHERE cust_id='$cust_id'";

            $result = mysql_query($query);

            //Making sure something was changed...

            if ((mysql_affected_rows90 == 0) || mysql_affected_rows() == -1)
                    {
                            echo "<p> There was a problem deleting...</p>";
                            exit;
                    }
    }
    echo "<p> The selected clients were successfully deleted!</p>";

}
?>

End of Code.

Can someone please tell what might be a problem here? Many thanks in advance.
Edit by admin: no contact info permitted on the forum, thank you

Again thank you to anyone trying to help me here.

    Instead of

    foreach($count=0; $count < count($_POST['cust_id']); $count++)

    Use the for function

    for($count=0; $count < count($_POST['cust_id']); $count++)

      hi
      I can find 3 syntax errors!
      Fix them.
      But not be too sure. You maybe have more ... bads 🙂

      First fix is on line 41, the other on line 52,
      and third is on line 58
      (your line 59, you probably have an empty line as line 1 .. please remove this topline)

      <input type="submit" name"submit" value="Delete Selected">
      if (isset($POST['submit']))
      $query = "DELETE FROM customer WHERE cust_id='$cust_id'";
      ... this $query I am not sure about. Your version may be correct.
      But this is how I do it, sometimes. I split the string.


      edit, after curb post:
      //Loop through each customer with selected checkbox
      foreach( $count=0; $count < count($_POST['cust_id']); $count++ )

      Should be:

      //Loop through each customer with selected checkbox
      for( $count=0; $count < count($_POST['cust_id']); $count++ )

      ------------------------------------------------------------------------------------------------

      This is the corrected code:

      <input type="submit" name="submit" value="Delete Selected">
      </FORM>
      <?php
      
      //Connect to the server and select the database
      
      mysql_connect("localhost", "analyst", "voyant");
      mysql_select_db("voyant");
      
      //Checking if the form has been submited
      
      if (isset($_POST['submit']))
      {
      //Loop through each customer with selected checkbox
      for ( $count=0; $count < count($_POST['cust_id']); $count++ )
      {
      $cust_id = $_POST['cust_id'][$count];
      $query = "DELETE FROM customer WHERE cust_id='" . $cust_id . "'";
      
      $result = mysql_query($query);
      
      //Making sure something was changed...
      
      if (mysql_affected_rows90 == 0 || mysql_affected_rows() == -1)
      {
      echo "<p> There was a problem deleting...</p>";
      exit;
      }
      }
      echo "<p> The selected clients were successfully deleted!</p>";
      }
        curb wrote:

        Instead of

        foreach($count=0; $count < count($_POST['cust_id']); $count++)

        Use the for function

        for($count=0; $count < count($_POST['cust_id']); $count++)

        That is the correct answer.
        Why you get error on line 59.
        Because there is no such 'counters', in a foreach.

        Only in FOR LOOP: for($i; $i<10; $i++){ }

        foreach( $array AS $key => $value ){ }

        But you should fix the 2 errors I found, too.
        else your script WILL NOT work.

        There is no such varaible $POST, for example,
        but $_POST ... is there, if post-<form> was submitted

        regars 🙂
        halojoy

          Thank you very much for everyones help!!

          I have made the fixes you guys have mentioned and found some other smaller bugs and typos on my own but am still having trouble getting what I need.

          Using this script I need to be able to delete from my database (seen below)

          +---------+-----------+--------------+------------+--------------------------+--------------------------+---------------------+
          | cust_id | cust_name | contact | phone | alert_email | report_email | date |
          +---------+-----------+--------------+------------+--------------------------+--------------------------+---------------------+

          all of the info from selected customer. So the user would be presented with a html table form which would display all of the customers and their info in the database row by row and each row would have a checkbox allowing the user to check it off and delete it from the database using the delete button.

          When I run the script and test it via the html form page I can select the customer and hit the delete button and it even tells me that I have successfully deleted the selected customers but when I check, they are not being deleted from the database...

          Also I get these errors:

          ##################################################
          <td><input type="checkbox" name="PHP Notice: Use of undefined constant cust_id - assumed 'cust_id' in /var/www/html/delete1.php on line 29
          12" value=""></td>
          <td>PHP Notice: Use of undefined constant cust_id - assumed 'cust_id' in /var/www/html/delete1.php on line 30
          12</td>
          <td>PHP Notice: Use of undefined constant cust_name - assumed 'cust_name' in /var/www/html/delete1.php on line 31
          dfgdfg</td>
          <td>PHP Notice: Use of undefined constant contact - assumed 'contact' in /var/www/html/delete1.php on line 32
          dfgdfgdfg</td>
          <td>PHP Notice: Use of undefined constant phone - assumed 'phone' in /var/www/html/delete1.php on line 33
          7575</td>
          <td>PHP Notice: Use of undefined constant alert_email - assumed 'alert_email' in /var/www/html/delete1.php on line 34
          dsdf@fgdfgdfg.juh</td>
          <td>PHP Notice: Use of undefined constant report_email - assumed 'report_email' in /var/www/html/delete1.php on line 35
          sdfsdf@ff.ny</td>
          <td>PHP Notice: Use of undefined constant date - assumed 'date' in /var/www/html/delete1.php on line 36
          2008-04-02 16:31:04</td>
          </tr>
          </TABLE>
          <input type="submit" name="submit" value="Delete Selected">
          </FORM>
          </BODY>
          </HTML>

          #################################################

          Here is the current code that I am using:

          <!-- Delete Customer Page -->
          <HTML>
          <HEAD> <TITLE> Delete Customer Page> </TITLE> </HEAD>
          <BODY>
          <FORM action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
          Deleting Customer Information Form.<br />
          Select all customers you want to delete from the database:
          <TABLE width="70%" border="0" cellpadding="2" cellspacing="2">
          <tr>
          <td width=5%></td>
          <td><strong>ID</strong></td>
          <td><strong>Customer</strong></td>
          <td><strong>Contact</strong></td>
          <td><strong>Phone</strong></td>
          <td><strong>Alerts</strong></td>
          <td><strong>Reports</strong></td>
          <td><strong>Date</strong></td>
          </tr>
          <?php
          mysql_connect("localhost", "analyst", "voyant");
          mysql_select_db("voyant");

          $query = "SELECT cust_id, cust_name, contact, phone, alert_email, report_email, date FROM customer ORDER BY cust_id";
          $result = mysql_query($query);
          while ($row = mysql_fetch_array($result))
          {
          ?>
          <tr>
          <td><input type="checkbox" name="<?php echo $row[cust_id]; ?>" value=""></td>
          <td><?php echo $row[cust_id]; ?></td>
          <td><?php echo $row[cust_name]; ?></td>
          <td><?php echo $row[contact]; ?></td>
          <td><?php echo $row[phone]; ?></td>
          </tr>
          <?php } ?>
          </TABLE>
          <input type="submit" name="submit" value="Delete Selected">
          </FORM>
          <?php
          //Connect to the server and select the database
          mysql_connect("localhost", "analyst", "voyant");
          mysql_select_db("voyant");
          //Checking if the form has been submited
          if (isset($POST['submit']))
          {
          //Loop through each customer with selected checkbox
          for($count=0; $count < count($
          POST['cust_id']); $count++)
          {
          $cust_id = $_POST['cust_id'][$count];
          echo "This is the customer ID $cust_id";
          $query = "DELETE FROM customer WHERE cust_id='$cust_id'";

                      $result = mysql_query($query);
          
                      //Making sure something was changed...
          
                      if ((mysql_affected_rows() == 0) || mysql_affected_rows() == -1)
                              {
                                      echo "<p> There was a problem deleting...</p>";
                                      exit;
                              }
              }
              echo "<p> The selected clients were successfully deleted!</p>";

          }
          ?>
          </BODY>
          </HTML>

          If anyone reading this has any ideas as to what is wrong here please tell me as I am totally new to php/mysql and at this point am stuck and don't know how to continue...

            Write a Reply...