Jump to content

mysqli delete


wright67uk

Recommended Posts

I'm having trouble with the below code.

 

It doesn't delete $horse, but at the same time still echo's "You have deleted " . $horse;

 

$horse echo's out fine, and i've run the query in phpmyadmin;

 

DELETE FROM races WHERE name = 'Abstract'      (one of the $horse names in my db) and the query deletes ok

 

How can I get the below query to delete, and how can I print out the statement below?

<?php session_start();

$horse = $_POST['horse'];
          
  $mysqli = new mysqli('','','',''); 

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if ($stmt = $mysqli->prepare("DELETE FROM races WHERE name = ? ")); 
{ 
$stmt->bind_param("s", $horse); 
$stmt->execute();
echo "You have deleted " . $horse;
}
 
echo '<br/><br/><a href="follow.php">Back</a><br/>';

print($mysqli->query);

$mysqli->close();

?>
Link to comment
Share on other sites

$stmt = $mysqli->prepare("DELETE FROM races WHERE name = ? ");
$stmt->bind_param("s", $horse); 
if ($stmt->execute())
{
echo "You have deleted " . $horse;
}
else {
echo "Failed to delete {$horse}<br>\r\n";
echo "[{$stmt->errno}] {$stmt->error}<br>\r\n";
}
That should spit out any error that is preventing it from deleting. Edited by kicken
Link to comment
Share on other sites

retire_process.php

<?php session_start();

$horse = $_POST['horse'];
          
  $mysqli = new mysqli('','','',''); 

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
    
// $mysqli->query("DELETE FROM notes WHERE horse_name = '$horse'");

$stmt = $mysqli->prepare("DELETE FROM races WHERE name = ? ");
$stmt->bind_param("s", $horse); 
if ($stmt->execute())
{
  echo "You have deleted " . $horse;
}
else {
  echo "Failed to delete {$horse}<br>\r\n";
  echo "[{$stmt->errno}] {$stmt->error}<br>\r\n";
}
 
echo '<br/><br/><a href="follow.php">Back</a><br/>';

?> 

 
Thanks for the suggestions for my code (retire_process.php), i've shown some more code as requested.

When I run the code you gave me, I'm still echoing "You have deleted A Bailer" (horse name)

I have checked mysql tables and they are are all using utf8_unicode_ci
'name' is set as a varchar.
 
 

extract of follow.php

$search = isset($_GET['search']) ? strtolower(trim($_GET['search'])) : '';
if(!in_array($search,$links)){
    // supplied search term is not an expected value
    $search = '';
}

// use the search value
if(empty($search)){
    // nothing selected
    echo "Please select something to search for.<br>";
} else {
    // perform the search
    echo "<br><br>";
    if($search == '0-9'){
        // special handling for a 0-9 leading wild-card search
        $query = "SELECT * FROM  `races` WHERE  `name` REGEXP '^[0-9]' GROUP BY NAME";
		$result = mysql_query($query);
    } else {
        $query ="SELECT name FROM  `races` WHERE  `name` LIKE  '$search%' GROUP BY name
                UNION
                SELECT horse_name FROM  `notes` WHERE  `horse_name` LIKE  '$search%' GROUP BY horse_name order by name";  
              
		$result = mysql_query($query);
    }
	

while($row = mysql_fetch_array($result))
  {
  echo '<div style="width:300px; float:left;">';
  echo $row['name'];
  echo '</div><div style="width:"300px; float:left">' ;

 echo '<a href="action.php?name=' .$row['name']. '">NapTrack /</a>' ;
 echo '<a href="info.php?horse=' .$row['name']. '">NapNotes </a>' ;

 $user_level = $_SESSION["user"]["user_level"];

 if ($user_level > 2) {
  echo '<form name="retire" action="retire.php" method="POST">
       <input type="hidden" name="horse" value=" ' .$row['name']. '">
       <input type="submit" value="Delete"></form>';
  }
  
  echo "</div><br/>";
  }

}
?> 

 
retire.php

<?php session_start() ;
$horse = $_POST['horse'];
?>

<html>
<form method="post" action="retire_process.php">
Are you sure you want to delete <?php echo $horse ?> ?
<input type="hidden" name="horse" value="<?php echo $horse ?>" />
<input type="submit" value="Yes" />
</form>
</html>

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.