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
https://forums.phpfreaks.com/topic/278262-mysqli-delete/
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.
Link to comment
https://forums.phpfreaks.com/topic/278262-mysqli-delete/#findComment-1431677
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
https://forums.phpfreaks.com/topic/278262-mysqli-delete/#findComment-1431760
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.