php page and mysql database

hello everyone I have been playing around with php and mysql. I have two pages running off an apache2 server. One is a forms page that inserts information and creates a database based on the information provided. It creates the database fine, however when I hit submit no table is created and the information isn’t transferred either. Also the insert.php is a blank page like a error page. here is my code.

This is the form page.

[php]

table of awesome!

<?php $con = mysql_connect("localhost","root","dietcoke"); if (!$con) { die('could not connect: ' . mysql_error()); }

if (mysql_query(“create database my_db”,$con))
{
echo “database created”;
}
else
{
echo "error creating database: " . mysql_error();
}
mysql_select_db(“my_db”, $con);
$sql = “create table Persons
(
P_Id int not null auto_increment,
firstname varchar(15),
lastname varchar(15),
streetline varchar(15),
streetline2 varchar(15),
city varchar(15),
state varchar(15),
zipcode varchar(15),
emailadd varchar(25),
cellnum varchar(15),
)”;

mysql_query($sql,$con);
mysql_close($con);
?>

first name:
last name:
Sreet line 1:
street line 2:
City:
State:
Zip Code:
email address:
Cell Number:
[/php]

this is the insert page

[php]<?php
$con = mysql_connect(“localhost”,“root”,“dietcoke”);
if (!$con)
{
die('could not connect: ’ . mysql_error());
}

mysql_select_db(“my_db”, $con);
$firstname = mysql_real_escape_string(trim($_post[‘firstname’]), $con);
$lastname = mysql_real_escape_string(trim($_post[‘lastname’]), $con);
$streetline = mysql_real_escape_string(trim($_post[‘streetline’]), $con);
$streetline2 = mysql_real_escape_string(trim($_post[‘streetline2’]), $con);
$city = mysql_real_escape_string(trim($_post[‘city’]), $con);
$state = mysql_real_escape_string(trim($_post[‘state’]), $con);
$zipcode = mysql_real_escape_string(trim($_post[‘zipcode’]), $con);
$emailadd = mysql_real_escape_string(trim($_post[‘emailadd’]), $con);
$cellnum = mysql_real_escape_string(trim($_post[‘cellnum’]), $con);

$sql="insert into persons (firstname, lastname, streetline, streetline2, city, state, zipcode, emailadd, cellnum)
values
(’$firstname’,’$lastname’,’$streetline’,’$streetline2’,’$city’,’$state’,’$zipcode’,’$emailadd’,’$cellnum’);
if (!mysql_query($sql,$con))
{
die('Error: ’ . mysql_error());
}
echo “1 record added”;

mysql_close($con);
?>[/php]

any help is greatly appreciated! :slight_smile:

Hi,

if you run the query for creating the table, you don’t check on mysql_error(). If you do, does it tell you more about what went wrong?
It might be helpful.

About the second part. Do you terminate your query string? It looks to me a " is missing and everything after
‘$sql="…’ is a string. :slight_smile:

Hope this helps,
O.

okay well i fixed up some of the syntax. I was missing a few brackets and a " like you said. Now my insert page is saying Error: Table ‘my_db.persons’ doesn’t exist.

Sponsor our Newsletter | Privacy Policy | Terms of Service