wright67uk Posted May 22, 2013 Share Posted May 22, 2013 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(); ?> Quote Link to comment Share on other sites More sharing options...
DaveyK Posted May 22, 2013 Share Posted May 22, 2013 You are checking if the statement is being prepared or not. Just because it is prepared correctly, doesnt mean its executed correxctly. You need to check the execute of the statement. Quote Link to comment Share on other sites More sharing options...
wright67uk Posted May 22, 2013 Author Share Posted May 22, 2013 Ok, thanks. How do I do that? Quote Link to comment Share on other sites More sharing options...
DaveyK Posted May 22, 2013 Share Posted May 22, 2013 perhaps: $stmt = $mysqli->prepare("DELETE FROM races WHERE name = ? "); if ($stmt->execute(array($horse))); { echo "You have deleted " . $horse; } Quote Link to comment Share on other sites More sharing options...
wright67uk Posted May 22, 2013 Author Share Posted May 22, 2013 Thanks for your help, although I still can't get my head around why its not deleting? Usually when my sql isnt executed, I would expect $stmt to be a non object. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 22, 2013 Share Posted May 22, 2013 how are you producing the value in $_POST['horse']? it might have a non-printing character as part of it that echoing it doesn't show. Quote Link to comment Share on other sites More sharing options...
kicken Posted May 22, 2013 Share Posted May 22, 2013 (edited) $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 May 22, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
wright67uk Posted May 23, 2013 Author Share Posted May 23, 2013 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> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 23, 2013 Share Posted May 23, 2013 your value=' ... ' has got a leading space in it (after the first " and before the ' ) <input type="hidden" name="horse" value=" ' .$row['name']. '"> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.