PHP not working. Insertion into SQL database

Here is all my code.
checkInfo.php

<?php

include ("config.php");
?>
<!doctype html>
<html>
<head>
<title>check</title>
</head>
<body>asdfasdf
<?php

$contentOfPost=$_POST['contentOfPost'];
$submit=$_POST['submit'];
$date = date("Y/m/d");
$time = time();

echo $date;
echo $time;
echo $submit;
echo "<br />";
echo $contentOfPost;

$link = mysql_connect($host, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
else{
echo 'Connected successfully';

$insertionToDatabase=INSERT INTO userSubmitted ('PostNumber', 'Date', 'Time', 'Content') VALUES (NULL, '$date', '$time', '$contentOfPost');

mysql_close($link);
}

?>

</body>
</html>



config.php

<?php
$host="mysql";
$username="thisisnthere";
$password="thisisfalse";
?>

blog.php

<?php
session_start();

?>
<!doctype html>
<html>
<head>
<title>blog</title>
<style type="text/css">

</style>
</head>
<body>
<?php
session_start();  
$_SESSION['views'] = 1; // store session data
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views']; 
?>
<form action="checkInfo.php" method="post">
<textarea name="contentOfPost">

</textarea>
<input type="submit" value="Submit Blog" name="submit" />
</form>
</body>
</html>



Basically this was working except when I tried to write the user submitted post into the database. No errors appear or anything, just a blank page. I’ve written some if/else statements to try and just get ANYTHING to appear but it’s a no go. What am I missing?

I know that the sql connection is correct because I’ve tested it before.

Crazy colored text, can hardly read that…

You are using PHP to build an SQL string. Did PHP do what you expected?

add this line:


echo $insertionToDatabase;

Look at the output very carefully, paste it into your database via your db management tool. Was it a valid SQL statement or not?

If no, then work backwards, echo out the PHP variables all the way back to the form if necessary - and check the syntax of your sql statement again.

If it was a valid SQL statement, then work forwards – why did it not reach your database? Double check your connections again, just because they used to work does not mean they still do. Hardcode the details into the script temporarily to identify what is going wrong where.

and yet still ~another~ developer kneecapped by the horrible mysql backticks

ryan, your list of column names that follows the table name is actually a list of strings

get rid of those quotes – they shoulda/coulda been backticks, but it’s best never to use backticks at all, ever

Hi, I tried b oth suggestions.

The issue is NOTHIGN is appearing. No text at all. Even if I hardcode HTML into the page.

I tried both suggestions but still nothign is appearing.

I go to blog.php, type in some random nonsense and hit submit. Upon it coming to checkInfo.php, it’s a blank page. Even no HTML shows up in the source.

i don’t do php, but i think your $insertionToDatabase needs some kind of quotes around the entire sql string, and also, don’t you need to run it into the mysql_query function?

anyhow, test the query outside of php first, to make sure it’s working

I don’t think my SQL insertion is working. I narrowed it down to my SQL insertion variable as being the cause for stuff not showing up.

Even if I go to INSERT in PHPmyadmin to give me a basic outline of the insertion SQL query, even when I copy that into my PHP file, it doesn’t work.

Basically I have four rows in my table userSubmittedData (in table blog) and the first row is auto incremented, second row is date, then time, then the actual content of the blog post.

INSERT INTO blog.userSubmitted (PostNumber, Date, Time, Content) VALUES (NULL, ‘2012-02-15’, CURRENT_TIMESTAMP, ‘thisisapost’);

That’s an example SQL query I got when I manually inserted something. It came with the ticks.

I think it is working now. The mysql_query was the issue, and the quotes.

I’ll give updates as I go along…

I’m now trying to get a loop going so it displays all of the posts I submit.

<?php
session_start();
include "config.php";
$i=0;
$query="SELECT PostNumber
FROM userSubmitted
ORDER BY PostNumber ASC";

$result=mysql_query($query);
if ($!result)
{echo "you suck";}
else{echo "yay";}

$num=mysql_numrows($result);



echo "<b><center>Database Output</center></b><br><br>";


while ($i < $num) {

$field1-name=mysql_result($result,$i,"PostNumber");
$field2-name=mysql_result($result,$i,"Date");
$field3-name=mysql_result($result,$i,"Time");
$field4-name=mysql_result($result,$i,"Content");

echo "<b>$field1-name
$field2-name2</b><br>$field3-name<br>$field4-name<br>$field5-name<hr><br>";

$i++;
}
mysql_close();
?>


Where am I wrong? Besides the noob HTML I put in there. I just was getting throwing it in there.

you can’t display 4 different columns if you only retrieve one column with your query

This is my updated code. For reference

asdf<?php
session_start();
include "config.php";
$link = mysql_connect($host, $username, $password);
$i=0;
$query="SELECT * FROM userSubmitted";


$result=mysql_query($query);
if (!$result)
{die("this didn't work");}
else{ echo "yay";
}


echo $result;
$num=mysql_num_rows($result);
echo $num;

//while ($i < $num) {
//
//$field1-name=mysql_result($result,$i,"PostNumber");
//$field2-name=mysql_result($result,$i,"Date");
//$field3-name=mysql_result($result,$i,"Time");
//$field4-name=mysql_result($result,$i,"Content");
//
//echo "<b>$field1-name
//$field2-name2</b><br>$field3-name<br>$field4-name<br>$field5-name<hr><br>";
//
//$i++;
//}
mysql_close();
?>


Output is “asdfthis didn’t work”

I fixed it all. Thanks.