monkeymaker Posted January 8, 2011 Share Posted January 8, 2011 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 https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/ Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2011 Share Posted January 8, 2011 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. Link to comment https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156681 Share on other sites More sharing options...
monkeymaker Posted January 8, 2011 Author Share Posted January 8, 2011 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 https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156684 Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2011 Share Posted January 8, 2011 There has to be a value, or the whole table will be truncated to an empty table. You have to identify the record somehow, preferably by its primary key index. Link to comment https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156685 Share on other sites More sharing options...
monkeymaker Posted January 8, 2011 Author Share Posted January 8, 2011 So, could the value be say, for instance, the ID? If so I have already tried this without luck? Link to comment https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156691 Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2011 Share Posted January 8, 2011 It sure could. Post the query you tried to use. Link to comment https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156698 Share on other sites More sharing options...
monkeymaker Posted January 8, 2011 Author Share Posted January 8, 2011 <?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 https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156701 Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2011 Share Posted January 8, 2011 Don't bump threads unless you're adding new information. Were you trying to use the above code with the function as it's currently written in the OP? Link to comment https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156727 Share on other sites More sharing options...
monkeymaker Posted January 8, 2011 Author Share Posted January 8, 2011 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 https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156729 Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2011 Share Posted January 8, 2011 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 https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156735 Share on other sites More sharing options...
monkeymaker Posted January 8, 2011 Author Share Posted January 8, 2011 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 https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156742 Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2011 Share Posted January 8, 2011 There's no way the query in the function you posted will delete anything as it's currently written. Link to comment https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156747 Share on other sites More sharing options...
Anti-Moronic Posted January 8, 2011 Share Posted January 8, 2011 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 https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156757 Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2011 Share Posted January 8, 2011 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 https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156762 Share on other sites More sharing options...
monkeymaker Posted January 8, 2011 Author Share Posted January 8, 2011 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 And yes you 'were' referring to me Link to comment https://forums.phpfreaks.com/topic/223782-sql-php-delete-help/#findComment-1156772 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.