1

Im trying to get the input from a form and delete the table data then display the the updated table but i get a blank page I don't know what could be the problem any help would be appericiated here's my code:

  <html>
        <body>
<?php
 $mysqli = new mysqli("xxxxx", "xxxxxx", "xxxxx", "xxxxxx");

 /* check connection */ 
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 //----------------------------------------------------------------------------------//
$name = $_POST['Car_ID'];

if ($stmt = $mysqli->prepare("delete from CARS where name=?")) {

    // Bind the variable to the parameter as a string. 
    $stmt->bind_param("s", $name);

    // Execute the statement.
    $stmt->execute();

 echo "Deleted data successfully\n";

    // Close the prepared statement.


  $mysqli->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $result = $db->prepare("SELECT id, Doors, TRANSMISSION, Fuel_type, Engine_Size, Total FROM CARS");
        $result->execute();
        while ($row = $result->fetch(PDO::FETCH_ASSOC)){
            $doors=$row["Doors"];
            $engine=$row["Engine_Size"];
            $total=$row["Total"];
            $trans=$row["Transmission"];
            }
         ?>

        <table>
        <tr>
        <td><?php echo $doors; ?></td>
        <td><?php echo $engine; ?></td>
        <td><?php echo $total; ?></td>
        <td><?php echo $trans; ?></td>

        </tr>
  <?php } ?>
        </table>
        </body>
        </html>

3 Answers3

5

You're mixing mysqli with PDO

$mysqli->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

and you're passing $db where it (theoretically) should be $mysqli

$result = $db->prepare("SELECT id, Doors, TRANSMISSION, Fuel_type, Engine_Size, Total, DATE_INITIATED, AGE, PARTNO, QTY, DESCRIPTION, LOC  FROM CARS");

where theoretically, it should be

$result = $mysqli->prepare("SELECT id, Doors, TRANSMISSION, Fuel_type, Engine_Size, Total, DATE_INITIATED, AGE, PARTNO, QTY, DESCRIPTION, LOC  FROM CARS");

since your DB connection is:

$mysqli = new mysqli("xxxxx", "xxxxxx", "xxxxx", "xxxxxx");

However, your DB connection however should resemble:

$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';

$db = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

You can't do this:

$stmt->bind_param("s", $name);

You're using PDO remember? (or are you?) Take your pick, is it mysqli or PDO?


You most likely want to do:

// $stmt = $db->prepare("delete from CARS where name=:value");

if ($stmt = $db->prepare("delete from CARS where name=:value")) {    
// Bind variables to your statement
$stmt->bindParam(':value', $name);

... }
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
2

Have you enabled error_reporting?

error_reporting(E_ALL);
ini_set("display_errors","on");

Add this to the head of your PHP File.

D. Schalla
  • 635
  • 4
  • 9
  • Then this shouldn't be the problem. There is the beginning missing, where you open the html + body, could you edit that into your prior post? – D. Schalla Apr 14 '14 at 15:43
  • yeah it is just the opening tags of both html and body before the php but i'll edit it in now – user3403781 Apr 14 '14 at 15:46
0

Great points made by @fred, when you finally decide which Database extensions you want to use, you will still have to fix the way you are outputting the table data, you should have the table tags outside of the loop, and you should be printing the table rows inside of the loop, watch out for extra braces.

something like this would work:

<html>
<body>
    <table>
<?php

   //Fetch your data
   //.....
   //....
    while ($row = $result->fetch(PDO::FETCH_ASSOC)){
    echo"<tr>
            <td>$row['Doors']</td>
            <td>$row['Engine_Size']</td>
            <td>$row['Total']</td>
            <td>$row['Transmission']</td>
        </tr>";
    }


?>
    </table>
</body>
</html>
Community
  • 1
  • 1
meda
  • 43,711
  • 13
  • 85
  • 120
  • 1
    Thanks for the mention. Just a quick note: you need to fix your quotes though. From `echo "` to `echo '` etc. or change `["Doors"]` to `['Doors']` etc. SO's syntax highlighting never lies ;-) – Funk Forty Niner Apr 14 '14 at 16:22
  • Yes, it's the little things that count ;-) Let's hope the OP's problem will be solved. – Funk Forty Niner Apr 14 '14 at 16:24