Timothy McGee
Timothy McGee

Reputation: 121

Error inserting into a database from a form

Hi I'm trying to insert some data from a form into my database but I can't seem to get it to work. I'm not getting any errors from the mysql_error() and I can't for the life of my figure out what could be wrong. Any help would be greatly appreciated, thanks in advance!

The code is outputting "Unsuccessful! Test 9".

Here's my form

<form method="post" action="blog_add_posts.php">
<input type="hidden" name="submitted" value="true" />

<fieldset>
    <legend>Add Blog Posts</legend>
    <label>Title: <input type="text" name="title" /></label>
    <label>Body: <input type="text" name="body" /></label>
</fieldset>
<br />

<input type="submit" value="Submit Post" />
</form>

Here's my PHP (I've hid the define statements for security reasons)

<?php
if (isset($_POST['submitted'])){
    define('DB_NAME', '(Hiding For Security)');
    define('DB_USER', '(Hiding For Security)');
    define('DB_PW', '(Hiding For Security)');
    define('DB_HOST', '(Hiding For Security)');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PW);

    if(!$link){
        die('Test Connection Failed 1: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if(!$db_selected){
        die('Test Connection Failed 1: ' . mysql_error());
    }

    $title = $_POST['title'];
    $title = mysqli_real_escape_string($link, $title);
    $body = $_POST['body'];
    $body = mysqli_real_escape_string($link, $body);
    $sqlInsert = "INSERT INTO 'phpmy1_belairfinishing_com'.'Blog' (Post_ID, Post_Title,     Post_Date, Post_Body) VALUES (NULL, '$title', CURRENT_TIMESTAMP, '$body')";

    if(mysqli_real_query($link, $sqlInsert)){
        echo "<p> Successful! </p>";
    } else {
        echo "<p> Unsuccessful! Test 11 </p>";
    }

    $newrecord = "Successfully Added To Database";

    mysqli_close($link);

} //End of ISSET if statement

?>

Upvotes: 1

Views: 47

Answers (3)

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

In your query you need to have your PHP variables outside the string, otherwise they will be parsed as text (ex: "$title") instead of the actual values of the variable:

$sqlInsert = "INSERT INTO 'phpmy1_belairfinishing_com'.'Blog' (Post_ID, Post_Title, Post_Date, Post_Body) VALUES (NULL, '".$title."', CURRENT_TIMESTAMP, '".$body."')";

Also in your form you need to have value so there is data to get:

<input type="text" name="title" value="" />
<input type="text" name="body" value="" />

This makes them blank at the start, make sure to type into them before hitting submit.

Along with this as @bhttoan pointed out. You need to make sure you are doing mysqli for all your functions and not mysql interlaced with mysqli.

Upvotes: 0

bhttoan
bhttoan

Reputation: 2736

You are mixing mysql_ and mysqli_

For example you have:

$link = mysql_connect(DB_HOST, DB_USER, DB_PW);

but then you use:

if(mysqli_real_query($link, $sqlInsert)){

You should use one or the other but you cannot mix them together like this

Upvotes: 2

Mostafa Mohsen
Mostafa Mohsen

Reputation: 786

<input type="submit" value="Submit Post" />

Change your HTML code to:

<input type="submit" name="submitted" value="Submit Post" />

and remove

<input type="hidden" name="submitted" value="true" />

And tell me how it goes.

Upvotes: 1

Related Questions