PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
josh22x
Forum Commoner
Posts: 38 Joined: Tue Feb 26, 2008 11:17 am
Post
by josh22x » Sat May 17, 2008 3:09 pm
Hey,
I am trying to submit from data from this form:
Code: Select all
<form action="blog2.php" method="post">
Your Name:<br> <input type="text" name="name" style="color: #000000; background-color: #E0E0E0"><br>
E-mail: <br><input type="text" name="email" style="color: #000000; background-color: #E0E0E0"><br>
Comments:<br><textarea name="comments" style="color: FFFFFF; background-color: #E0E0E0;
font-size= 18px" cols = "80" rows = "30" >
</textarea><br><br>
<input type="submit" style="color: #000000; background-color: #E0E0E0" value="Add New Post">
</form>
Into my database with this php code:
***** PLEASE DO NOT POST PASSWORDS *****
Code: Select all
<?php
// create short variable names
$name=$_POST['name'];
$email=$_POST['email'];
$comments=$_POST['comments'];
if (!$name || !$email || !$comments)
{
echo 'You have not entered all the required details.<br />'
.'Please go back and try again.';
exit;
}
$db = mysql_connect('localhost', 'root', 'xxxxxxxx');
mysql_select_db('blog');
$db = mysql_connect('localhost', 'root', 'xxxxxxxx');
if (!$db) {
(die . 'Could not connect' . mysql_error());
}
$link = mysql_select_db('blog');
if ($link)
echo 'Connected';
else
echo 'not connected';
echo '<br></br>';
$sql = ("INSERT INTO contents VALUES (1,'name', 'email', 'comments';");
$query = mysql_query($db);
$query = mysql_query($sql);
if($query)
echo $db->affected_rows.' book inserted into database.';
else
echo 'Data not entered!';
?>
Everytime i click submit is runs through the code and displays:
Why will it not submit the data to the database?
Thanks!!
califdon
Jack of Zircons
Posts: 4484 Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA
Post
by califdon » Sat May 17, 2008 8:58 pm
Looks like the MySQL host to which you are connecting doesn't have a database named 'blog'.
csand
Forum Newbie
Posts: 6 Joined: Wed May 14, 2008 6:00 pm
Post
by csand » Sat May 17, 2008 11:05 pm
No I think the problem is the query.
Code: Select all
$sql = ("INSERT INTO contents VALUES (1,'name', 'email', 'comments';");
I'm guessing your columns/fields are name, email, and comments so the query should read
Code: Select all
$sql = "INSERT INTO contents (name,email,comments) VALUES ('name', 'email', 'comments')";
I'm fairly new to coding php/mysql, but I think this should solve your problem.
califdon
Jack of Zircons
Posts: 4484 Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA
Post
by califdon » Sun May 18, 2008 11:28 am
csand wrote: No I think the problem is the query.
Code: Select all
$sql = ("INSERT INTO contents VALUES (1,'name', 'email', 'comments';");
I'm guessing your columns/fields are name, email, and comments so the query should read
Code: Select all
$sql = "INSERT INTO contents (name,email,comments) VALUES ('name', 'email', 'comments')";
I'm fairly new to coding php/mysql, but I think this should solve your problem.
Actually, you're right, at least partly. Sorry, I didn't read his code carefully enough. You're right about the query, but also he has two mysql_query() calls where there should be only one, and he's missing brackets in his if blocks, plus several other syntax errors. It needs to look like this:
Code: Select all
$db = mysql_connect('localhost', 'root', 'xxxxxxxx');
mysql_select_db('blog');
$db = mysql_connect('localhost', 'root', 'xxxxxxxx') or die ('Could not connect! ' . mysql_error());
$link = mysql_select_db('blog');
if ($link) {
echo 'Connected';
} else {
echo 'not connected';
echo '<br />';
$sql = "INSERT INTO contents ('name', 'email', 'comments') VALUES ($name, $email, $comments)";
$query = mysql_query($sql);
if($query) {
echo $db->affected_rows.' book inserted into database.';
} else {
echo 'Data not entered!';
}
?>
Josh, you really need to study basic PHP syntax. A good reference is
http://w3schools.com .