View Full Version : This should be easy...PHP/MySQL
joseph@josephflahiff.com
10-28-2007, 03:36 PM
Help, What the heck am I doing wrong? I get the error that it can't execute my sql code (an error I wrote in after reading some of these posts-thanks all)
Any help appreciated.
<html>
<head>
<title>test</title>
</head>
<body>
<?php if ($submit) {
// process form
$db = mysql_connect("localhost", "somedatabase","notreal") or die ("couldn't connect to server");
mysql_select_db("dealapag_Dealapage",$db) or die ("Couldn't select database");
$sql = "INSERT INTO users (Index,first,last,email,addr1,addr2,city,state,zip ) VALUES (NULL,$first,$last,$email,$addr1,$addr2,$city,$sta te,$zip)";
$result = mysql_query($sql) or die ("couldn't execute sql insert");
echo "Thank you! Information entered.";
} else{
// display form
?>
<form method="post" action="<?php echo $PHP_SELF?>">First
name:<input name="first" type="text"><br>
Last name:<input name="last" type="text"><br>
Email:<input name="email" text="" type=""><br>
Address1:<input name="addr1" type="text"><br>
Address2:<input name="addr2" type="text"><br>
City:<input name="city" type="text"><br>
State:<input name="state" type="text"><br>
Zip:<input name="zip" type="text"><br>
<input name="submit" value="Enter information"
type="submit">
</form>
<?php } // end if
?>
</body>
</html>
redsox9
10-28-2007, 05:44 PM
What does the error tell you?
Just at first glance I see this:
$db = mysql_connect("localhost", "somedatabase","notreal") or die ("couldn't connect to server");
mysql_select_db("dealapag_Dealapage",$db) or die ("Couldn't select database");
$sql = "INSERT INTO users (Index,first,last,email,addr1,addr2,city,state,zip ) VALUES (NULL,$first,$last,$email,$addr1,$addr2,$city,$sta te,$zip)";
$result = mysql_query($sql) or die ("couldn't execute sql insert");
PHP might not like the fact that you have the single quote inside the double quotez. You may want to proceed the single quote with a backwards slash (\).
joseph@josephflahiff.com
10-28-2007, 10:04 PM
I don't get an error message.
It just runs but I get no record added to my database. When I added the "or die..." lines i found it was dieing on $result = ... somewhere.
Is there some way to put it into verbose mode so it will talk to me? :-)
felgall
10-28-2007, 11:14 PM
You haven't included any code in the page to read the posted values back into the variables that the call to the database uses.
Basil
10-29-2007, 04:55 AM
<?php
echo('<html>
<head>
<title>test</title>
</head>
<body>
');
if (isset($_POST['submit'])) {
// process form
$db = mysql_connect("localhost", "somedatabase","notreal") or die ("couldn't connect to server");
mysql_select_db("dealapag_Dealapage",$db) or die ("Couldn't select database");
$sql = "INSERT INTO users (first,last,email,addr1,addr2,city,state,zip) VALUES (".$_POST['first'].",".$_POST['last'].",".$_POST['email'].",".$_POST['addr1'].",".$_POST['addr2'].",".$_POST['city'].",".$_POST['state'].",".$_POST['zip'].")";
$result = mysql_query($sql) or die ("couldn't execute sql insert");
echo "Thank you! Information entered.";
} else {
// display form
echo('<form method="post" action="">First
name:<input name="first" type="text"><br/>
Last name:<input name="last" type="text"><br/>
Email:<input name="email" text="" type=""><br/>
Address1:<input name="addr1" type="text"><br/>
Address2:<input name="addr2" type="text"><br/>
City:<input name="city" type="text"><br/>
State:<input name="state" type="text"><br/>
Zip:<input name="zip" type="text"><br/>
<input name="submit" value="Enter information"
type="submit">
</form>');
}
echo('
</body>
</html>');
?>
Forgive me for not testing it, I'd rather not set up a database right now.
joseph@josephflahiff.com
10-31-2007, 07:52 AM
Hi All
Thanks for the replies. I added the post statements. And I like the approach where you embed the HTML in the PHP rather than embed the PHP in the HTML seems cleaner somehow...
but unfortunately it still dies on the sql execute and I don't get a record in my database.....
I checked and the account does have rights to the database... any ideas?
here is the webpage if that helps
www.dealapage.com/order.php
Thanks
Peace
Basil
10-31-2007, 10:52 AM
Change $result = mysql_query($sql) or die ("couldn't execute sql insert"); to $result = mysql_query($sql) or die (mysql_error());
Otherwise I have no idea what the error even is.
joseph@josephflahiff.com
10-31-2007, 03:30 PM
Ok added the Mysql_error line.
Have a look....
thank you so much for all your help everyone!!
joseph@josephflahiff.com
10-31-2007, 05:01 PM
Ok!
Progress.
It doesn't work when I enter text
but I entered a number in each field and it worked.
I checked the Database and sure enough they are all there.
So why doesn't the "values" field accpet text?
Peace
Basil
10-31-2007, 09:07 PM
Alright, sorry, the problem was that you didn't have quotes around the variables you were inserting into the database.
I added some checks to make sure the variables exist and to prevent injections, it should actually work right this time, honest!
<?php
echo('<html>
<head>
<title>test</title>
</head>
<body>
');
$first = isset($_POST['first'])&&!empty($_POST['first'])?mysql_escape_string($_POST['first']):null;
$last = isset($_POST['last'])&&!empty($_POST['last'])?mysql_escape_string($_POST['last']):null;
$email = isset($_POST['email'])&&!empty($_POST['email'])?mysql_escape_string($_POST['email']):null;
$addr1 = isset($_POST['addr1'])&&!empty($_POST['addr1'])?mysql_escape_string($_POST['addr1']):null;
$addr2 = isset($_POST['addr2'])&&!empty($_POST['addr2'])?mysql_escape_string($_POST['addr2']):null;
$city = isset($_POST['city'])&&!empty($_POST['city'])?mysql_escape_string($_POST['city']):null;
$state = isset($_POST['state'])&&!empty($_POST['state'])?mysql_escape_string($_POST['state']):null;
$zip = isset($_POST['zip'])&&!empty($_POST['zip'])?mysql_escape_string($_POST['zip']):null;
if (isset($_POST['submit'])) {
// process form
$db = mysql_connect("localhost", "root","password") or die ("Couldn't connect to server");
mysql_select_db("dealapag_Dealapage",$db) or die ("Couldn't select database");
$sql = "INSERT INTO users (first,last,email,addr1,addr2,city,state,zip) VALUES ('$first','$last','$email','$addr1','$addr2','$cit y','$state','$zip')";
$result = mysql_query($sql) or die (mysql_error());
echo "Thank you! Information entered.";
} else {
// display form
echo('<form method="post" action="">
First name:<input name="first" type="text"/><br/>
Last name:<input name="last" type="text"/><br/>
Email:<input name="email" type="text"/><br/>
Address1:<input name="addr1" type="text"/><br/>
Address2:<input name="addr2" type="text"/><br/>
City:<input name="city" type="text"/><br/>
State:<input name="state" type="text"/><br/>
Zip:<input name="zip" type="text"/><br/>
<input name="submit" value="Enter information" type="submit"/>
</form>');
}
echo('
</body>
</html>');
?>
joseph@josephflahiff.com
11-01-2007, 10:55 AM
Hey it works.
thanks so very very much!!
www.dealapage.com/order.php
Peace
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.