-1

I am getting a white screen of death with no errors anywhere. var_dump() does not seem to help.

    <?php
    $host = "localhost";
    $user = "custdb";
    $pass = "ms";
    $db = "accounting";
    $odb = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
    //insert customer
    if (isset($_POST['name'])) {
            $cust_name = $_POST['cust_name'];
            $cust_addr = $_POST['cust_addr'];
            $cust_phone = $_POST['cust_phone'];
            $cust_email = $_POST['cust_email'];
            $q = "INSERT INTO customer (cust_name,cust_addr,cust_phone,cust_email)
                            SELECT * FROM (SELECT cust_name) as tmp WHERE NOT EXISTS (SELECT cust_name FROM customer WHERE cust_name = '1');";
            $query = $odb->prepare($q);
            var_dump($cust_name);
            $results = $query->execute(array(
                    ":cust_name" => $cust_name,
                    ":cust_addr" => $cust_addr,
                    ":cust_phone" => $cust_phone,
                    ":cust_email" => $cust_email,
            ));
    var_dump($cust_addr);
    var_dump($results);
    }
    ?>


<!DOCTYPE html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title> Contacts Database </title>
        </head>

        <body>
                <form methd="post" action"">
                        Name: <input type="text" id="cust_name" /><br />
                        Address: <input type="text" id="cust_addr" /><br />
                        Phone: <input type="text" id="cust_phone" /><br />
                        Email: <input type="text" id="cust_email" /><br />
                        <input type ="submit" value"add" />
                <form>
        <?php
                $query = "SELECT * FROM customer";
                $result = $odb->query($query);
                if($result->rowCount() > 0) {
                        foreach($result as $item) {
                                echo($item['cust_name'] .",". $item['cust_addr'] .",". $item['cust_phone'] .",". $item['cust_email']."<br />\n");
                        }
                }

        ?>

****************** output *******************

Name: Address: Phone: Email: bob,666 bo rd,2124442222,bo@bo.com

https://i.stack.imgur.com/Kn9lE.png

enter code here

************ sql cli *******************

mysql> INSERT INTO customer (cust_name,cust_addr,cust_phone,cust_email )
    -> SELECT * FROM (SELECT 'bob', '666 bo rd','2124442222','bo\@bo.com') AS tmp
    -> WHERE NOT EXISTS (
    -> SELECT cust_name FROM customer WHERE cust_name='bob')
    -> LIMIT 1;
Query OK, 1 row affected, 1 warning (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from customer;                                                  +----+-----------+-----------+------------+------------+
| id | cust_name | cust_addr | cust_phone | cust_email |
+----+-----------+-----------+------------+------------+
|  1 | bob       | 666 bo rd | 2124442222 | bo@bo.com  |
+----+-----------+-----------+------------+------------+
1 row in set (0.00 sec)
  • Set `display_errors = On` and `error_reporting = E_ALL` in your `php.ini` file and restart your web server. I'm not sure how you expect to use `$odb` before you've defined it. This should definitely trigger some errors. – Phil Feb 24 '14 at 03:09
  • Also, what on Earth is up with that `INSERT` statement? This is the second, weird `INSERT...SELECT...WHERE NOT EXISTS...` query I've seen [in as many weeks](http://stackoverflow.com/q/21869234/283366). Is there some really bad tutorial being circulated that people are following lately? – Phil Feb 24 '14 at 03:12
  • I sql works perfectly well on the command line. the two php statements were out of order because it's actually the header. –  Feb 24 '14 at 03:15
  • I don't care if it *works*; it's just stupid. If you want to enforce unique-ness, use a unique constraint. That's what they're for. You also have bound, named parameters without placeholders. This will also trigger errors. I suggest setting PDO to throw exceptions – Phil Feb 24 '14 at 03:16
  • 1
    You're getting this (*so called*) "white screen of death", most likely because of your un-named form elements. **In particular**, your `if (isset($_POST['name']))` conditional statement is looking for a form element named `name`, as in `` which is not in your posted code. It doesn't find it, so it fails. Had you an `else{echo "Nothing is set";}` then that would have shown you, that nothing is set. – Funk Forty Niner Feb 24 '14 at 03:29
  • ...along with quite a few other mistakes. Missing equals sign, the word `method` which was spelled `methd`. – Funk Forty Niner Feb 24 '14 at 03:54

1 Answers1

0

EDIT

It seems like you did not have any VALUES for your query. The following has been tested and working on my server.

N.B.: I'm having trouble understanding what it is you want to achieve with this line:

$q = "INSERT INTO customer (cust_name,cust_addr,cust_phone,cust_email) 
SELECT * FROM (SELECT cust_name) as tmp 
WHERE NOT EXISTS (SELECT cust_name FROM customer WHERE cust_name = '1');";

Plus, I made a few additions, one of which being bindParam for all values.

<?php
    $host = "localhost";
    $user = "custdb";
    $pass = "ms";
    $db = "accounting";
    $odb = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);

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

    //insert customer
    if (isset($_POST['submit'])) {
            $cust_name = $_POST['cust_name'];
            $cust_addr = $_POST['cust_addr'];
            $cust_phone = $_POST['cust_phone'];
            $cust_email = $_POST['cust_email'];

            $q = "INSERT INTO customer (cust_name,cust_addr,cust_phone,cust_email) 

VALUES (
        :cust_name, 
        :cust_addr, 
        :cust_phone, 
        :cust_email)";

        $query = $odb->prepare($q);

$query->bindParam(':cust_name', $_POST['cust_name'], PDO::PARAM_STR);       
$query->bindParam(':cust_addr', $_POST['cust_addr'], PDO::PARAM_STR); 
$query->bindParam(':cust_phone', $_POST['cust_phone'], PDO::PARAM_STR);
$query->bindParam(':cust_email', $_POST['cust_email'], PDO::PARAM_STR);

            var_dump($cust_name);

            $query->execute(array(
                    ':cust_name' => $cust_name,
                    ':cust_addr' => $cust_addr,
                    ':cust_phone' => $cust_phone,
                    ':cust_email' => $cust_email,
            ));

if($query != false) {
    echo "Data successfully entered into database.";
} else {
    echo "An error occured saving your data.";
}
    var_dump($cust_addr);
    var_dump($results);
    }
?>

<!DOCTYPE html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title> Contacts Database </title>
        </head>

        <body>

<form method="post" action="">
        Name: <input type="text" id="cust_name" name="cust_name" /><br />
        Address: <input type="text" id="cust_addr" name="cust_addr" /><br />
        Phone: <input type="text" id="cust_phone" name="cust_phone" /><br />
        Email: <input type="text" id="cust_email" name="cust_email" /><br />
        <input type ="submit" name="submit" value="add" />
<form>

<?php
        $query = "SELECT * FROM customer";
        $result = $odb->query($query);
        if($result->rowCount() > 0) {
                foreach($result as $item) {
                        echo($item['cust_name'] .",". $item['cust_addr'] .",". $item['cust_phone'] .",". $item['cust_email']."<br />\n");
                }
        }

?>

Original answer

I posted this as a comment, but I've made it as an answer (along with a few other syntax errors that were spotted upon closer inspection).

You're getting this (so called) "white screen of death", most likely because of your un-named form elements.

In particular, your if (isset($_POST['name'])) conditional statement is looking for a form element named name, as in <input type="text" name="name"> which is not in your posted code. It doesn't find it, so it fails.

Had you an else{echo "Nothing is set";} then that would have shown you, that nothing is set.

All of your form elements need to be named, otherwise, none of the values will be entered in DB, since you are using variables $cust_name etc. from POST variables which are to come from your form, and those do not have name="element_name" for example.

A few other things that I spotted is the missing = in <input type ="submit" value"add" /> which should read as <input type ="submit" value="add" /> that has been fixed below and the missing = in <form methd="post" action""> and the missing o for method that was spelled methd which should be <form method="post" action="">.

For example:

<form method="post" action="">
        Name: <input type="text" id="cust_name" name="cust_name" /><br />
        Address: <input type="text" id="cust_addr" name="cust_addr" /><br />
        Phone: <input type="text" id="cust_phone" name="cust_phone" /><br />
        Email: <input type="text" id="cust_email" name="cust_email" /><br />
        <input type ="submit" value="add" />
<form>

And I suggest you check if your submit button is set, instead of the name.

I.e.:

<input type ="submit" name="submit" value="add" />

and change your conditional statement from:

if (isset($_POST['name'])) to if (isset($_POST['submit'])) then use an else condition as an escape route.

Your form action is set to self. Using if (isset($_POST['submit'])) is better to use in a case like this.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141