Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?
You are not logged in.
Pages: 1
**I know, MySQL has insecurities. I'm using it because I'm trying to learn a little bit of PHP and it seemed easy to mess with.
------
I was messing around with a PHP login/register, then seen that I can delete rows in my table using PHP. I downloaded a script that was supposed to delete them, and made a few changes. It displays my table rows perfectly, but it won't delete them. I've tried so many different things and I keep getting an SQL error.
The Error I Keep Getting
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
index.php
<body> <?php //mysql connection $dbconn = mysql_connect("BANANA","APPLES","MANGOS") or die(); mysql_select_db("GRAPEFRUIT") or die(); $sqlquery = "SELECT * FROM tablename"; // query on table $sqlresult = mysql_query($sqlquery, $dbconn); $count = mysql_num_rows($sqlresult); // count query result ?> <form method="post" action="delete.php"> <table> <tr> <td>#</td> <td>Account ID</td> <td>Username</td> <td>Password</td> <td>Rank</td> </tr> <?php //loop here while($row = mysql_fetch_array($sqlresult)) { //inside while ?> <tr> <td><input type="checkbox" name="checkbox[]" id="checkbox[]" value="<?php echo $row['id']?>" /></td> <td><?php echo $row['member_id']?></td> <td><?php echo $row['login']?></td> <td><?php echo $row['passwd']?></td> <td><?php echo $row['rank']?></td> </tr> <?php } ?> <tr> <td> <input id='delete' type='submit' name='delete' value='Delete'/> </td> </tr> </table> </form> </body>
delete.php
<?php //mysql connection here require_once('profile.php'); if($_POST['delete']) { $checkbox = $_POST['checkbox']; //from name="checkbox[]" $countCheck = count($_POST['checkbox']); for($i=0;$i<$countCheck;$i++) { $del_id = $checkbox[$i]; $sql = "DELETE FROM tablename WHERE id = $del_id"; $result = mysql_query($sql, $dbconn); } if($result) { echo "successful delete"; } else { echo "Error: ".mysql_error(); } } ?>
Discord: jawp#5123
Offline
try doing
"DELETE FROM tablename WHERE id = '$del_id'"
*u stinky*
Offline
As long as the code is not in the production (live) environment, turn on PHP error reporting.
error_reporting (E_ALL); ini_set ('display_errors', '1');
This will show you that, if delete.php says "require_once('profile.php');", you need a profile.php file. The comment above the require_once line says "//mysql connection here", so profile.php needs to contain the two lines that are under "//mysql connection" in your index.php, plus of course the PHP delimiters (<?php ?>).
If you already had a profile.php, you should mention its contents when asking for help, to make sure we get a complete picture of your setup/problem. This also goes for the database content. The best thing would be to give all the SQL statements to create the sample database, like this:
CREATE DATABASE `GRAPEFRUIT`; USE `GRAPEFRUIT`; CREATE TABLE `tablename` (id BIGINT UNSIGNED UNIQUE NOT NULL AUTO_INCREMENT, member_id BIGINT UNSIGNED UNIQUE NOT NULL, login VARCHAR(200) UNIQUE NOT NULL, passwd VARCHAR(200) NOT NULL, rank INT(10) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `tablename` VALUES (NULL, '1', 'user1', 'pass1', '1'); INSERT INTO `tablename` VALUES (NULL, '2', 'user2', 'pass2', '2');
But it would also be okay to paste something like this:
+----+-----------+-------+--------+------+ | id | member_id | login | passwd | rank | +----+-----------+-------+--------+------+ | 1 | 1 | user1 | pass1 | 1 | | 2 | 2 | user2 | pass2 | 2 | +----+-----------+-------+--------+------+
When you turn on error reporting, you'll also see that the original MySQL API is deprecated, so you should use MySQLi statements instead.
I fixed the syntax error. Now I'm getting a new error...
Error: Unknown column 'id' in 'where clause'
I don't run into that error. If you still get it, you'll need to re-paste the exact code you're currently using and also what the contents is of your sample database table.
By the way, a better place to ask for coding help is probably Stack Overflow.
I have permanently left the game and forum.
EE is an entertaining game and I enjoyed playing it...
...but it's time for me to move on.
Offline
Pages: 1
[ Started around 1738901141.0141 - Generated in 0.063 seconds, 12 queries executed - Memory usage: 1.45 MiB (Peak: 1.59 MiB) ]