Jump to content

SQL PHP Delete help


monkeymaker

Recommended Posts

Hello, the user selects an id (sid) on select form then presses confirm button. Below is my delete function however I have error :

 

Warning: Missing argument 5 for dbstudent::delete_student(), called in C:\xampp\htdocs\cw1\delstudent.php on line 36 and defined in C:\xampp\htdocs\cw1\studentfunc.php on line 28

SQL Insertion 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 '(sid, name, address, postcode, photo) from student where values ('1', '', '', '' at line 1

 

Please advise!!

 

     function delete_student($sid, $name, $address, $postcode, $photo) {
        $esc_name = mysql_real_escape_string($name, $this->conn);
        $esc_address = mysql_real_escape_string($address, $this->conn);
        $esc_postcode = mysql_real_escape_string($postcode, $this->conn);
        $esc_photo = mysql_real_escape_string($photo, $this->conn);
        $sql = "delete (sid, name, address, postcode, photo) from student where
values ('{$sid}', '{$esc_name}', '{$esc_address}', '{$esc_postcode}', '{$esc_photo}')";

        $result = mysql_query($sql, $this->conn);

        if (!$result) {
            die("SQL Insertion error: " . mysql_error());
        } else {
            $numofrows = mysql_affected_rows($this->conn);
            return $numofrows;
        }
    }

Link to comment
Share on other sites

Wrong syntax for a DELETE query.

DELETE FROM `table` WHERE `some_field` = 'some_value'

 

However, if you're trying to set those fields to be empty, and not delete the record entirely, you'll need to use an UPDATE query.

 

Thanks, but there isnt a value I just want to delete the contents on an entire row from when the user presses 'confirm' button? sorry of any of this sounds dumb

Link to comment
Share on other sites

<?php include 'studentfunc.php'; ?>
<?php
$db1 = new dbstudent();
$db1->openDB();
$sql="select sid from student";
$result=$db1->getResult($sql);// get the ids from the tables for the select
?>

<?php
if (!$_POST) //page loads for the first time
{
?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
Select Student ID to <b> DELETE! </b>: <select name="sid">
<?php
while($row = mysql_fetch_assoc($result))
echo "<option value='{$row['sid']}'>{$row['sid']} </option>";
?>
</select>
<input type="submit" value=">!DELETE!<" />
</form>

<?php
}

else
{
$sid = $_POST['sid'];
$name = $_POST['name'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$photo = $_POST['photo'];
$db1 = new dbstudent();
$db1->openDB();
$numofrows = $db1->delete_student($sid, $cid, $grade, $comments);
echo "Success. Number of rows affected:
<strong>{$numofrows}<strong>";


$db1->closeDB();
}
?>

Link to comment
Share on other sites

yeah, everything is the same..what I really don't get though is that the SQL : delete from (table) where value ='anything' is a manual thing only... for instance I have a select form, user selects the ID to delete, then after pressing the button the SQL query should pick up that the ID has been selected and removes the associated rows and ID?

 

Basically having to type in the manual SQL instead of using the select form is pointless really and all the examples I have seen thats what they basically say to do ?

Link to comment
Share on other sites

You have to specify which record to delete, or every record in the table will be deleted. The database doesn't just magically know which record's id is stored in a variable in the php script, so you have to tell it.

 

Additionally, as I pointed out earlier, the syntax in your DELETE query is wrong. Something along these lines is what you're going to need to work with.

 

function delete_student($sid) {
        if( intval($sid) > 0 ) {
                return false;
        } else {
        $sql = "DELETE FROM `student` WHERE `sid` = $sid LIMIT 1";
   $result  . . . etc.

Link to comment
Share on other sites

Huh? I appreciate the help but I want to point out that your example youve just shown me is waaaaayy more advanced and complicated then neccessary, adding to the confusion of learning something new, actually I just did iwhat I wanted with a buddies help. Dude, seriously are you trying to scare me away from php or something, really? lol

Link to comment
Share on other sites

Huh? I appreciate the help but I want to point out that your example youve just shown me is waaaaayy more advanced and complicated then neccessary, adding to the confusion of learning something new, actually I just did iwhat I wanted with a buddies help. Dude, seriously are you trying to scare me away from php or something, really? lol

 

scare you away from php? YES! Professional, competent developers are sick of seeing sloppy code all over the place produced by cowboy programmers who don't know what they're doing....and I'm NOT referring to you. It's highly doubtful Pikachu is doing anything but trying to help you as best he can.

 

You have to also understand that sometimes we have to write code 'defensively' because we are not the ones testing it in your environment. For that reason, it might appear overcomplicated. It is not. This is php 101. If you're having trouble with this, consider reading up on some beginner tutorials.

Link to comment
Share on other sites

I don't know how I can possibly say it any more simply than this:

 

1) If you do not specify which record(s) to delete, and do not use a LIMIT clause, all records in the table will be deleted.

2) The query in the function will not work as written. It is a syntactical disaster.

Link to comment
Share on other sites

I don't know how I can possibly say it any more simply than this:

 

1) If you do not specify which record(s) to delete, and do not use a LIMIT clause, all records in the table will be deleted.

2) The query in the function will not work as written. It is a syntactical disaster.

 

Literally, this is all that was neccessary...

 

$sql = "DELETE FROM student WHERE sid = $sid";

 

Maybe something got lost in translation so to speak in terms of all I was trying to do?

 

I started off doing C++ and I quickly understood that the more simple solutions were the best solutions.

 

Also I appreciate the concern but all this stuff about cowboy coding seems uncalled for, all I wanted help for was with a delete command, not an CMS written in PHP  :D And yes you 'were' referring to me :D

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.