Jump to content

PHP Form to MySQL


jeliot

Recommended Posts

This is my first post and yes I am a newbie but I did alot of research before posting.... so here it goes

I have a test script wich will populate my db
and I have a test script wich will then take the data make it viewable in a browser...but.....
this email form script

<?php
$db_host = "host_name";
$db_user = "my_user_name";
$db_pwd = "3456789";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST[‘name’];
$email = $_POST[‘email’];
mysql_query("INSERT INTO `email_list` (name, email, ) VALUES ('$name', ‘$email’, )");
}
?>


won't populate the data to my db


Any Help with this would be greatly appreciated I've been racking my brain







Link to comment
Share on other sites

[!--quoteo(post=360553:date=Apr 1 2006, 12:29 AM:name=jeliot)--][div class=\'quotetop\']QUOTE(jeliot @ Apr 1 2006, 12:29 AM) [snapback]360553[/snapback][/div][div class=\'quotemain\'][!--quotec--]
This is my first post and yes I am a newbie but I did alot of research before posting.... so here it goes

I have a test script wich will populate my db
and I have a test script wich will then take the data make it viewable in a browser...but.....
this email form script

<?php
$db_host = "host_name";
$db_user = "my_user_name";
$db_pwd = "3456789";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST[‘name’];
$email = $_POST[‘email’];
mysql_query("INSERT INTO `email_list` (name, email, ) VALUES ('$name', ‘$email’, )");
}
?>
won't populate the data to my db
Any Help with this would be greatly appreciated I've been racking my brain
[/quote]



You dont need a coma after your last values on the query. so change this
[code]mysql_query("INSERT INTO `email_list` (name, email, ) VALUES ('$name', ‘$email’, )");[/code]

to this
[code]mysql_query("INSERT INTO `email_list` (name, email) VALUES ('$name', ‘$email’)");[/code]


Also, You wanna put the sql query in a variable, so you can check it.
so instead of this:
[code]mysql_query("INSERT INTO `email_list` (name, email, ) VALUES ('$name', ‘$email’, )");[/code]

Do this:
[code]
$query =  "INSERT INTO `email_list` (name, email) VALUES ('$name', ‘$email’ )";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
[/code]


That should do it and if not it will print out an error message. If it does, let us know what it was
Link to comment
Share on other sites

[!--quoteo(post=360558:date=Apr 1 2006, 02:06 AM:name=play_)--][div class=\'quotetop\']QUOTE(play_ @ Apr 1 2006, 02:06 AM) [snapback]360558[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You dont need a coma after your last values on the query. so change this
[code]mysql_query("INSERT INTO `email_list` (name, email, ) VALUES ('$name', ‘$email’, )");[/code]

to this
[code]mysql_query("INSERT INTO `email_list` (name, email) VALUES ('$name', ‘$email’)");[/code]
Also, You wanna put the sql query in a variable, so you can check it.
so instead of this:
[code]mysql_query("INSERT INTO `email_list` (name, email, ) VALUES ('$name', ‘$email’, )");[/code]

Do this:
[code]
$query =  "INSERT INTO `email_list` (name, email) VALUES ('$name', ‘$email’ )";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
[/code]
That should do it and if not it will print out an error message. If it does, let us know what it was
[/quote]


Ok thanks for the info I'm a step closer i think........the query created this error to which I could not figure a solution.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '‘ )' at line 1
Link to comment
Share on other sites

[!--quoteo(post=360614:date=Apr 1 2006, 10:23 AM:name=jeliot)--][div class=\'quotetop\']QUOTE(jeliot @ Apr 1 2006, 10:23 AM) [snapback]360614[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Ok thanks for the info I'm a step closer i think........the query created this error to which I could not figure a solution.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '‘ )' at line 1
[/quote]

you dont need the ! in your (!isset .... try it without that so "(isset"
Link to comment
Share on other sites

you have back-ticked around $email in your query

change
[code]
mysql_query("INSERT INTO `email_list` (name, email) VALUES ('$name', ‘$email’)");
[/code]

to
[code]
mysql_query("INSERT INTO `email_list` (name, email) VALUES ('$name', '$email')");
[/code]

and you should be fine...
Link to comment
Share on other sites

[!--quoteo(post=360705:date=Apr 1 2006, 04:59 PM:name=alpine)--][div class=\'quotetop\']QUOTE(alpine @ Apr 1 2006, 04:59 PM) [snapback]360705[/snapback][/div][div class=\'quotemain\'][!--quotec--]
you have back-ticked around $email in your query

change
[code]
mysql_query("INSERT INTO `email_list` (name, email) VALUES ('$name', ‘$email’)");
[/code]

to
[code]
mysql_query("INSERT INTO `email_list` (name, email) VALUES ('$name', '$email')");
[/code]

and you should be fine...
[/quote]

Thanks guys you have been great I'm getting there........now the script is just creating empty field in my database...here's my revised script

<head>
<title>Join Mailing List</title>
</head>

<body>
<?php
$db_host = "mysql";
$db_user = "jeliot";
$db_pwd = "8013301";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$query = "INSERT INTO `email_list` (name, email) VALUES ('$name', '$email' )";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
}
?>

I'm going to do some research if anyone knows the solution right off thanks in advance
Link to comment
Share on other sites

Try this
[code]<?php
$db_host = "localhost";
$db_user = "root";
$db_pwd = "";
$db_name = "tester";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
if (!isset($_POST['submit']))
    {

echo '<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>';

} else {
$name = $_POST['name'];
$email = $_POST['email'];
$query = "INSERT INTO email_list (name,email) VALUES ('".$name."','".$email."')";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
}
?>[/code]
Link to comment
Share on other sites

Also, you dont need to split up your code like that.

This looks cleaner:

[code]


<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>


<?php
$db_host = "mysql";
$db_user = "jeliot";
$db_pwd = "8013301";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);

if (isset($_POST['submit'])) {
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$query = "INSERT INTO `email_list` (name, email) VALUES ('$name', '$email' )";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
}
?>

[/code]

aaaaalso, instead of typig out:
[code]
$db_host = "mysql";
$db_user = "jeliot";
$db_pwd = "8013301";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
[/code]
every time you wanna connect to the database, you can put it in a separate file and call that file. this makes your code shorta & cleaner.

so you could make connect.php and put this there:
[code]
$db_host = "mysql";
$db_user = "jeliot";
$db_pwd = "8013301";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
[/code]

and then, on the email page you call it.
[code] inlclude('./connect.php');[/code].

So to summerize, here is the final thing:

[code]
<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>


<?php
if (isset($_POST['submit'])) {
inlclude('./connect.php');
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$query = "INSERT INTO `email_list` (name, email) VALUES ('$name', '$email' )";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
}
?>
[/code]
Link to comment
Share on other sites

All great info I've been looking to try to neaten up my script

What I meant by creating a field is when id use my download script it shows me a new field but with no data....almost like submitting an empty form (if that was possible)

In my database I have 1 table with 3 fields

ID INT no / null auto increment primary
name VARCHAR no / null
email VARCHAR no / null

i'm not sure if the problem is with how the db is set up or not but i just changed the ID field to include auto increment and primary key and now my populate.php won't populate my db any more so I've gone a little backwards

Thanks for the help............. i'm still chuggin a long
Link to comment
Share on other sites

[!--quoteo(post=360747:date=Apr 1 2006, 06:26 PM:name=jeliot)--][div class=\'quotetop\']QUOTE(jeliot @ Apr 1 2006, 06:26 PM) [snapback]360747[/snapback][/div][div class=\'quotemain\'][!--quotec--]
All great info I've been looking to try to neaten up my script

What I meant by creating a field is when id use my download script it shows me a new field but with no data....almost like submitting an empty form (if that was possible)

In my database I have 1 table with 3 fields

ID INT no / null auto increment primary
name VARCHAR no / null
email VARCHAR no / null

i'm not sure if the problem is with how the db is set up or not but i just changed the ID field to include auto increment and primary key and now my populate.php won't populate my db any more so I've gone a little backwards

Thanks for the help............. i'm still chuggin a long
[/quote]

Ok i figured out that if i take out the ID field I recently added in my db then my populate.php WILL populate my db with test names but................

my email form has stoped adding emtpy rows and has reverted to sending nothing but i'm getting no errors
I'm starting to dig myself deeper
Link to comment
Share on other sites

[quote name='jeliot' date='Apr 1 2006, 06:48 PM' post='360751']
Ok i figured out that if i take out the ID field I recently added in my db then my populate.php WILL populate my db with test names but................

my email form has stoped adding emtpy rows and has reverted to sending nothing but i'm getting no errors
I'm starting to dig myself deeper
[/quote]

Ok i figured out that if i take out the ID field I recently added in my db then my populate.php WILL populate my db with test names but................


[/quote]

my old email_form script

<?php
$db_host = "mysql";
$db_user = "jeliot";
$db_pwd = "8013301";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$query = "INSERT INTO `email_list` (name, email) VALUES ('$name', '$email' )";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
}
?>
still sends the form with the Name: empty and Email: empty

my revised script (with play's help)

<?php
$db_host = "mysql";
$db_user = "jeliot";
$db_pwd = "8013301";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$query = "INSERT INTO `email_list` (name, email) VALUES ('$name', '$email' )";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
}
?>
doesn't error but doesn't send anything to the database
Link to comment
Share on other sites

Change
[code]$name = $_POST[‘name’];
$email = $_POST[‘email’];[/code]

to:

[code]$name = $_POST['name'];
$email = $_POST['email'];[/code]

Also change:
[code]$query = "INSERT INTO `email_list` (name, email) VALUES ('$name', '$email' )";[/code]

To:

[code]$query = "INSERT INTO email_list (name,email) VALUES ('".$name."','".$email."')";[/code]

Link to comment
Share on other sites

[!--quoteo(post=360761:date=Apr 1 2006, 07:36 PM:name=jeliot)--][div class=\'quotetop\']QUOTE(jeliot @ Apr 1 2006, 07:36 PM) [snapback]360761[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Ok i figured out that if i take out the ID field I recently added in my db then my populate.php WILL populate my db with test names but................
my old email_form script

<?php
$db_host = "mysql";
$db_user = "jeliot";
$db_pwd = "8013301";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$query = "INSERT INTO `email_list` (name, email) VALUES ('$name', '$email' )";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
}
?>
still sends the form with the Name: empty and Email: empty

my revised script (with play's help)

<?php
$db_host = "mysql";
$db_user = "jeliot";
$db_pwd = "8013301";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST[‘name’];
$email = $_POST[‘email’];
$query = "INSERT INTO `email_list` (name, email) VALUES ('$name', '$email' )";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
}
?>
doesn't error but doesn't send anything to the database
[/quote]

That's not my revised script :D. I separated all the php and html in half, instead of having php, html and php again.


here, try this one:
[code]

<form action="#" method="post">
Name: <input type="text" name="name" size="20"><br>
Email: <input type="text" name="email" size="20"><br>
<input type="submit" name="submit" value="Submit!">
</form>


<?php

if (isset($_POST['submit'])) {
inlclude('./connect.php');
$name = $_POST['name'];
$email = $_POST['emai'];
$query = "INSERT INTO email_list (name, email) VALUES ('$name', '$email')";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
if ($result) {
echo 'This is email:'.$email.'<br>';
echo 'This is name:'.$name.'<br>';
} else {
echo 'Failed to add:'.$email.' to database<br>';
echo 'Failed to add:'.$name.' to database<br>';
}

?>[/code]

Make connect.php and put it on the same directory as your email script, and put this inside:
[code]
$db_host = "mysql";
$db_user = "jeliot";
$db_pwd = "8013301";
$db_name = "email";
mysql_connect($db_host, $db_user, $db_pwd) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
[/code]
Try that and let me know.

Also, for the varchars columns in your database table, you did give a number of allowed input right? And for int also?
Link to comment
Share on other sites

I tried that script and the form comes up blank I did notice the with my first version of the script if i took out the
! berfore the isset IT TO WOULD display a blank page

Thats all I have so far I'm still messing around
Link to comment
Share on other sites

do this to echo out the query to help us:

[code]
if (isset($_POST['submit'])) {
inlclude('./connect.php');
$name = $_POST['name'];
$email = $_POST['emai'];
$query = "INSERT INTO email_list (name, email) VALUES ('$name', '$email')";
$result = mysql_query($query) or die(mysql_error()); // this performs the actual query.
echo $query;
if ($result) {
echo 'This is email:'.$email.'<br>';
echo 'This is name:'.$name.'<br>';
} else {
echo 'Failed to add:'.$email.' to database<br>';
echo 'Failed to add:'.$name.' to database<br>';
}
[/code]
Link to comment
Share on other sites

[!--quoteo(post=360785:date=Apr 1 2006, 09:48 PM:name=jeliot)--][div class=\'quotetop\']QUOTE(jeliot @ Apr 1 2006, 09:48 PM) [snapback]360785[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I tried that script and the form comes up blank I did notice the with my first version of the script if i took out the
! berfore the isset IT TO WOULD display a blank page

Thats all I have so far I'm still messing around
[/quote]


I've got my form script working thanks to ALL that helped
the only problem i have is the script isn't short and organized
i still need some help cleaning up the script i'm going to post another forum with my working script and my revised cleaner script that isn't working............
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.