Search code examples
phpforum

php goes to blank page, no errors, but does not input infi in database


Thanks for your help, let me know if you need anything else. details in the title.

<?php

$subject = $_POST['subject'];
$comment = $_POST['comment'];
$submit = $_POST['submit'];

if ($submit)

{
$connect = mysql_connect("host","un","psw");
mysql_select_db("rebeler_comment");

$query = mysql_query("INSERT INTO table VALUES('','$subject','$comment')");
}


?>

<form action="form.php" method="POST">
<label>Subject</label></br>
<input type="text" name="subject"</br>
<label>Comment</label></br>
<textarea name="comment"></textarea></br>
<input type="submit" name="submit" value="Submit">

updated with my html


Solution

  • Firstly, the use of MySQL_ is deprecated. Use MySQLi_ and/or PDO.

    Now, you're not specifiying "where" to put your data in your table.

    Assuming your columns are named subject and comment respectively.

    Also, the word table is a reserved word. Therefore if your table is indeed called table, you need to wrap in inside back ticks,

    like this: `table`
    

    $query = mysql_query("INSERT INTO table (`subject`, `comment`) 
    VALUES ('$subject','$comment')");
    

    If table name is called "table":

    Use:

    $query = mysql_query("INSERT INTO `table` (`subject`, `comment`) 
    VALUES ('$subject','$comment')");
    

    Deleted '', from ('','$subject','$comment') because you only have 2 values going in DB.

    You may even want to concatenate such as:

    VALUES ('" . $subject . "','" . $comment . "')");
    

    To echo a success message:

    $query = mysql_query("INSERT INTO `table` (`subject`, `comment`) 
    VALUES ('$subject','$comment')");
    
    echo "Data successfully written to DB";
    }
    
    else{
    echo "Sorry, there was a problem.";
    }
    

    EDIT 2:

    <?php
    $subject = $_POST['subject'];
    $comment = $_POST['comment'];
    
    if(isset($_POST['submit']))
    {
    $connect = mysql_connect("host","un","psw");
    mysql_select_db("rebeler_comment");
    
    $query = mysql_query("INSERT INTO `table` (`subject`, `comment`) VALUES ('" . $subject . "','" . $comment . "')");
    $retval = mysql_query( $query, $connection ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); }
    echo "Entered data successfully\n";
    mysql_close($connection);
    }
    ?>
    

    EDIT 1:

    $query = mysql_query("INSERT INTO `table` (`subject`, `comment`) VALUES ('" . $subject . "','" . $comment . "')");
    $retval = mysql_query( $query, $connection ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); }
    echo "Entered data successfully\n";
    mysql_close($connection);