Jump to content

I get An error when trying to delete from Database in my Plugin


Jenksuy
Go to solution Solved by Jenksuy,

Recommended Posts

Hello there, 

 

I have written some php code to try and delete records from my module when they are selected and deleted. However when i go to delete the record i get the following error...

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

Here is my code

 

Delete.php

<?php 
    $db_name = "...";		// The database we created earlier in phpMyAdmin.
    $db_server = "...";	// Change if you have this hosted.
    $db_user = "...";		// Your USERNAME	
    $db_pass = "..."; 			// Your PASSWORD. Working locally, mine is blank. 
    $mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error());
     if($_POST["delete"]) // from button name="delete"
         {
              $checkbox = $_POST["checkbox"]; //from name="checkbox[]"
              $countCheck = count($_POST["checkbox"]);
              for($i=0;$i<$countCheck;$i++)
             {
                 $del_id  = $checkbox[$i];
                 $sql = "DELETE from job_details where id = $del_id";
                 $result = $mysqli->query($sql) or die(mysqli_error($mysqli));
                echo $del_id;
             }

} ?>

backend.php

function delete_a_job() {
    $con = mysql_connect("...","...","...");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
     
    mysql_select_db("...", $con);
     
    $result = mysql_query("SELECT * FROM job_details");
     
    echo '<form method="post" action="/wp-content/plugins/face-recruiter/delete.php">
    <table border="1">
    <tr>
    <th>Delete</th>
    <th>Job Reference</th>
    <th>Job Title</th>
    <th>Category</th>
    <th>Location</th>
    <th>Salary</th>
    <th>Type</th>
    </tr>';
     
    while($row = mysql_fetch_array($result))
      {
          echo '<tr>';
          echo '<td><input type="checkbox" name="checkbox[]" id="checkbox[]"  value='.$row['JobReference'].' />';
          echo '<td><a href="/details.php?id='.$row['JobReference'].'">' . $row['JobReference'] . '</a></td>';
          echo '<td>' . $row['JobTitle'] . '</td>';
          echo '<td>' . $row['Category'] . '</td>';
          echo '<td>' . $row['Location'] . '</td>';
          echo '<td>' . $row['Salary'] . '</td>';
          echo '<td>' . $row['Type'] . '</td>';
          echo '</tr>';
      }
    echo "</table>";
    
    // when the loop is complete, close off the list.
   echo '<p><input id="delete" type="submit" class="button" name="delete" value="Delete Selected Items"/></p></form>';
     
    mysql_close($con);
    
}

Can Anyone see what I am doing wrong??

Edited by Jenksuy
Link to comment
Share on other sites

  • Solution

Thanks... the following code worked which I have updated above...

$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error());
     if($_POST["delete"]) // from button name="delete"
         {
              $checkbox = $_POST["checkbox"]; //from name="checkbox[]"
              $countCheck = count($_POST["checkbox"]);
              for($i=0;$i<$countCheck;$i++)
             {
                 $del_id  = $checkbox[$i];
                 $sql = "DELETE from job_details where jobReference = $del_id";
                 $result = $mysqli->query($sql) or die(mysqli_error($mysqli));
                echo $del_id;
             }
          }

I added the for loop and changed id to jobReference in the query as that what $del_id was showing!

 

Thanks

Link to comment
Share on other sites

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.