0

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]);
    }



    ?>      
9
  • Do you have php installed on your webserver? Commented Dec 12, 2013 at 18:06
  • 2
    Do you see any error? Commented Dec 12, 2013 at 18:07
  • Does the ftp user designed by $db_username has INSERT rights? Commented Dec 12, 2013 at 18:07
  • Note that this method is deprecated (mysql_). So that might be the problem. Also missing quotation marks at your query string Commented Dec 12, 2013 at 18:08
  • Missing double quotes here: $query = "DELETE FROM test WHERE avail='$avail'"; Commented Dec 12, 2013 at 18:09

3 Answers 3

1

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]);
    }



    ?>    
Sign up to request clarification or add additional context in comments.

1 Comment

And here comes the embarrassment... Thanks a ton!
1

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');

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.