Meehee
Meehee

Reputation: 11

PHP unable to write to MySQL database

I have gone over my code many many MANY times and added any missing brackets or semi-colons but still whenever I upload this code to my website and load the page I still get a completely blank screen. The code was from an O'Reilly book so I went and checked the website if there are any reported errors in the book but found nothing related to this particular example.

I don't feel like it's an issue with permissions because I think the page would at least report one of the errors I coded into it. Could it have to do with the versions of PHP or MySQL I am using? I was able to connect to the database in the past and query it but writing just isn't happening. I am at a complete loss at this point. All I want to do is write to my MySQL database and party :(

Here is the code:

    <?php
    require_once 'login.php';

    // Create connection
    $db_server = mysql_connect($db_hostname, $db_username, $db_password);

    // Check connection
    if (!$db_server) die("Unable to connect  to MySQL: " . mysql_error());

    mysql_select_db($db_database, $db_server)
        or die("Unable to select database: " . mysql_error());

    if (isset($_POST['delete']) && isset($_POST['avail']))
    {
        $avail = get_post('avail');
        $query = DELETE FROM test WHERE avail='$avail';

        if (!mysql_query($query, $db_server))
            echo "DELETE failed: $query<br />" .
            mysql_error() . "<br /><br />";
    }       

    if (isset($_POST['id']) &&
        isset($_POST['item_name']) &&
        isset($_POST['avail']))
    {
        $id         = get_post('id');
        $item_name  = get_post('item_name');
        $avail      = get_post('avail');

    $query = "INSERT INTO test VALUES" .
            "('$id','$item_name','$avail')";

    if (!mysql_query($query, $db_server))
        {echo "INSERT failed: $query<br />" .
        mysql_error() . "<br /><br />";
    }
    }

    ?>
    <form action="index.php" method="post"><pre>
        Line Number         <input type="text" name="id" />
        Product Name        <input type="text" name="item_name" />
        Quantity Available  <input type="text" name="avail" />
        <input type="submit" value="ADD RECORD" />
    </pre></form>
    <?php


    $query  = "SELECT * FROM test";
    $result = mysql_query($query);

    if (!$result) die ("Database access failed: " . mysql_error());
    $rows = mysql_num_rows($result);

    for ($j = 0 ; $j < $rows ; ++$j)
    {
        $row = mysql_fetch_row($result);
        ?>
    <pre>
        Line Number $row[0]
        Product Name $row[1]
        Quantity Available $row[2]
    </pre>
    <form action="index.php" method="post">
    <input type="hidden" name="delete" value="yes" />
    <input type="hidden" name="avail" value="$row[2]" />
    <input type="submit" name="DELETE RECORD" /></form>
    <?php
    }

    mysql_close($db_server);

    function get_post($var)
    {
        return mysql_real_escape_string($_POST[$var]);
    }



    ?>      

Upvotes: 0

Views: 833

Answers (3)

Leo Bali
Leo Bali

Reputation: 311

you have error in the delete statment , try out this code :

 <?php
    require_once 'login.php';

    // Create connection
    $db_server = mysql_connect($db_hostname, $db_username, $db_password);

    // Check connection
    if (!$db_server) die("Unable to connect  to MySQL: " . mysql_error());

    mysql_select_db($db_database, $db_server)
        or die("Unable to select database: " . mysql_error());

    if (isset($_POST['delete']) && isset($_POST['avail']))
    {
        $avail = get_post('avail');
        $query = "DELETE FROM test WHERE avail='$avail'";

        if (!mysql_query($query, $db_server))
            echo "DELETE failed: $query<br />" .
            mysql_error() . "<br /><br />";
    }       

    if (isset($_POST['id']) &&
        isset($_POST['item_name']) &&
        isset($_POST['avail']))
    {
        $id         = get_post('id');
        $item_name  = get_post('item_name');
        $avail      = get_post('avail');

    $query = "INSERT INTO test VALUES" .
            "('$id','$item_name','$avail')";

    if (!mysql_query($query, $db_server))
        {echo "INSERT failed: $query<br />" .
        mysql_error() . "<br /><br />";
    }
    }

    ?>
    <form action="index.php" method="post"><pre>
        Line Number         <input type="text" name="id" />
        Product Name        <input type="text" name="item_name" />
        Quantity Available  <input type="text" name="avail" />
        <input type="submit" value="ADD RECORD" />
    </pre></form>
    <?php


    $query  = "SELECT * FROM test";
    $result = mysql_query($query);

    if (!$result) die ("Database access failed: " . mysql_error());
    $rows = mysql_num_rows($result);

    for ($j = 0 ; $j < $rows ; ++$j)
    {
        $row = mysql_fetch_row($result);
        ?>
    <pre>
        Line Number $row[0]
        Product Name $row[1]
        Quantity Available $row[2]
    </pre>
    <form action="index.php" method="post">
    <input type="hidden" name="delete" value="yes" />
    <input type="hidden" name="avail" value="$row[2]" />
    <input type="submit" name="DELETE RECORD" /></form>
    <?php
    }

    mysql_close($db_server);

    function get_post($var)
    {
        return mysql_real_escape_string($_POST[$var]);
    }



    ?>    

Upvotes: 1

apartridge
apartridge

Reputation: 1820

Please ensure that you are displaying errors.

ini_set("display_errors", "1");

You can also create a new page with just

phpinfo();

to check that PHP is running (and what PHP configuration you have).

Upvotes: 0

ewein
ewein

Reputation: 2735

If no errors are being displayed add the following to the top of your file, it will allow for errors to be shown:

error_reporting(E_ALL);
ini_set('display_errors', '1');

Upvotes: 1

Related Questions