2

I've spent today going through tons of similar questions and trying to figure out what is wrong with my code, lots of issues people had with back ticks, quotes, etc but none seem to help or change my cause. My code is no producing any errors, but when I use echo to print out my query results, it seems that the id is not getting a value.

In my delete.php:

<?
ini_set('display_errors',"1");

$username="xxx";
$password="xxx";
$database="xxx";

$conn = new mysqli(localhost, $username, $password, $database);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$id = (int)$_GET['number'];
mysqli_query($conn,"DELETE FROM tourdates WHERE id=".$id."");

$conn->close();

?>

And the delete button in my main.php (the rest of the php is correctly displaying my table with data):

<td><a href='delete.php?number='".$row['id']."'>Delete</a></td>

Can someone help pick out what is causing my rows not to delete when I hit the delete button that I have created, or maybe something that more clearly can help me debug? (I don't want to use checkboxes for this).

EDIT:

I also tried this code (while defining the function as $sql and I'm getting a "Success" message:

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

EDIT 2:

I changed the structure following the advice that I should use POST, thinking I might have caught something I didn't notice before, but still not working.

echo "<td><form method='post' action='delete.php'>
        <input type='hidden' name='row_id' value=".$row['id']."  />
        <input type='submit' name='delete_row' />
        </form>";

-

if(isset($_POST['delete_row'])) {
    $stmt = $conn->prepare("DELETE FROM tourdates WHERE ID = ?");
    $stmt->bind_param('i', $_REQUEST['row_id']);
    $stmt->execute();
}

If I do it the above way, nothing happens. Also tried this way, and get a syntax error:

if(isset($_POST['delete_row'])) {
    $id = $_POST['row_id'];
    $sql = "DELETE FROM tourdates WHERE id=".$id;
    mysqli_query($conn,$sql);
}
5
  • The error you are controlling is from the connection not the query. Check out the documentation php.net/manual/en/mysqli-stmt.error.php Commented May 27, 2015 at 15:46
  • Define the query in a simple string, print it to see what it contains and then pass it to the mysql_query. Like this : $query = "DELETE ..."; echo $query; $conn->query($query);. Commented May 27, 2015 at 15:51
  • In case the string prints what it should I think you must follow @jeroen's answer. Commented May 27, 2015 at 15:55
  • What is the value of $_REQUEST['row_id'] in your last edit? Commented May 27, 2015 at 16:49
  • @jeroen I'm only getting a / character. Commented May 27, 2015 at 17:16

4 Answers 4

4

A potential problem that I can see, is that you are not quoting localhost so php will look for a constant called localhost:

$conn = new mysqli('localhost', $username, $password, $database);
                   ^         ^ here

You are also not checking for errors so that is why you don't see any. The easiest way to fix that, is to have mysqli throw exceptions. Just add this to the top of your script:

mysqli_report(MYSQLI_REPORT_STRICT);

I also don't know if you can mix procedural and object oriented mysqli like that. You should probably stick to the OOP version.

Apart from that you should not use a link (GET request) for your delete actions. What if a web-crawler or a browser extension tries to fetch the links? Instead you should use a POST request (like a form with a button).

Edit: There is another problem which causes you not to get your ID and as you cast it to int, you will always get 0:

<td><a href='delete.php?number='".$row['id']."'>Delete</a></td>
                               ^ Oooops, closing the href attribute value here...

Your id gets placed after the value / outside of the quote of the href value. You can easily verify this if you look at the source of your page.

You need:

<td><a href='delete.php?number=".$row['id']."'>Delete</a></td>
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the tip with the localhost quotes. However, the mysql_report is also not returning any errors. I'm going to try to switch back to an <input> for my delete button, although earlier I couldn't get that way to work either.
@Eichhörnchen No errors at all? And if you use mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);? Note that this needs to be on the top of the script, before any mysqli calls.
@Eichhörnchen That is to be expected, you are not outputting anything. Are you sure the record with that ID exists? What is the value of $conn->affected_rows? That is the number of rows deleted. Note that a successful query can result in 0 rows deleted, that is not an error.
Assuming that your MySQL statements fail somewhere and you did not set properly your logging configuration, if you have access to your web server, you can look at the Apache (or your equivalent) error logs file: you'll find out which is the error causing your blank page.
@jeroen ah ok, so it said 0 affected rows when I click delete.
|
0

Replace these two parts of code in your php file, first write your host in the quotations

$conn = new mysqli('localhost', $username, $password, $database);

in your where condition you wrote id=".$id."" replace it with id=".$id

write it as:

mysqli_query($conn,"DELETE FROM tourdates WHERE id=".$id);

Edited:

If you want to see error in your query then use the below code:

mysqli_query($conn,"DELETE FROM tourdates WHERE id=".$id) or die(mysqli_error($conn));

2 Comments

Unfortunately this didn't help. Still not deleting and not returning an error
to see the error in your query use the code from my edited part
0

why not use try and catch to see your error?

anyways try this

$stmt = $conn->prepare("DELETE FROM tourdates WHERE ID = ?");<br>
$stmt->bind_param('i', $_REQUEST['number']);<br>
$stmt->execute(); 

Comments

-1

could this be the problem ?

$id = (int)$_GET['number'];

May be this would be better... ?

$id = intval($_GET['number']);

Anyway if, echo($query) print an empty id, this is probably because your parameter is not an integer.

3 Comments

(int)$_GET['number'] is a valid syntax. See the PHP documentation.
@vard you're right, I m sorry and thank you for your answer. So could this be the prblem ?: $id = (int)$_GET['number']; mysqli_query($conn,"DELETE FROM tourdates WHERE id=".$id.""); look at the sql query, don't the double quotes around $id cause the issue, in that way sql will think it's a string. Am I wrong ?
Well it indeed doesn't make sense as id is not a string field in the database, though MySQL is pretty permissive as it does the type conversion itself if the input data isn't of the type of the field, at some performance cost. The only case when this could bring an issue would be if MySQL run in strict mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.