Page 1 of 2
Input from form is not going into mysql table
Posted: Wed Oct 06, 2010 5:45 pm
by lilpenguin
I just noticed this as I looked in the mysql table that I made in phpmyadmin. My table values increment (with id_user auto increment) ok but when i submit my form with the values being variables passed to php script, they do not save into the table. However, I get no php error when the script is ran and the output is as expected.
Here is my code for the form. (connection to server and database have been established at this point)
Code: Select all
<?php
$sql="INSERT INTO users (
name ,
username ,
password ,
confirm ,
email ,
offer ,
music
)
VALUES
(
'$_POST[name]',
'$_POST[username] ',
'$_POST[password] ',
'$_POST[confirm] ',
'$_POST[email] ',
'$_post[offer] ',
'$_post[music] '
)";
if (!mysql_query($sql,$conn))
{
die('Error: ' . mysql_error());
}
echo "You have been added to the site.";
?>
The values are not showing up in the table when i view the table in phpmyadmin. They are blank. It may be worthy to note that I did a google search and discovered that the id_user )(which I'm using as primary key) may need to be put int he code despite it autoincrementing automatically, but I tried that and still got same result.
Thanks in advance!
Re: Input from form is not going into mysql table
Posted: Wed Oct 06, 2010 5:46 pm
by lilpenguin
and unrelated, why is my code not showing up in little boxes like it's supposed to? I copied and pasted the tags so I would not get my famous typos:)
Re: Input from form is not going into mysql table
Posted: Wed Oct 06, 2010 6:11 pm
by califdon
The first thing to check is whether you have any values in the $_POST array. The easy way to do this is to echo the entire $sql variable before you attempt to do the Insert operation. Often this will immediately reveal the problem.
You are exposing your database and your entire website to serious malicious damage by not "cleansing" the data from the $_POST array before using it to update your database! Look into using mysql_real_escape_string() function or other techniques. Using the raw data as you are is just asking for a hacker to wipe out your database!
You definitely DO NOT need to include the auto-increment column in your Insert, in fact that will usually guarantee an error.
I don't understand your second post question. What "little boxes" are you referring to??????????
Re: Input from form is not going into mysql table
Posted: Wed Oct 06, 2010 6:18 pm
by lilpenguin
For the sec ond question, never mind. I thought my code did not post right in the forum but it is ok now.
Ok, back to the code, do you mean I have to place the echo statement above the insert statement? Forgive me in advance but maybe I'm missing something
Re: Input from form is not going into mysql table
Posted: Wed Oct 06, 2010 6:36 pm
by califdon
lilpenguin wrote:For the sec ond question, never mind. I thought my code did not post right in the forum but it is ok now.
That's because I edited your post to remove the spaces that you had in the tags (but I didn't fix your second one, so you could see what's wrong).
Ok, back to the code, do you mean I have to place the echo statement above the insert statement? Forgive me in advance but maybe I'm missing something
Right. It's the first-line-of-defense debugging practice. If you don't know what values are in your variables, it's pretty hard to figure out why something isn't working, so temporarily print them out on your screen until you've found the problem, then remove the echo statements. Understand, this is for debugging, when something isn't working right. It's only to help you figure out what's happening in your script. Otherwise, you're like a blind man on a tightrope.
Re: Input from form is not going into mysql table
Posted: Wed Oct 06, 2010 8:12 pm
by lilpenguin
ok so I took out the insert and entered in echo statements. That way I can just view what I submitted in the form when I hit submit.
So new code I entered in is
[syntax="php]
echo $_POST["name"];
echo $_POST["username"];
echo $_POST["password"];
echo $_POST["confirm"];
echo $_POST["email"];
echo $_POST["offer"];
echo $_POST["music"];
[/syntax]
I get nothing on the next page..it is just blank. That means that the variables are not being filled up when in fact they should be.

Re: Input from form is not going into mysql table
Posted: Wed Oct 06, 2010 8:41 pm
by califdon
Ahh, see how much you can learn from tracing what's in the variables? Now you know that you have to determine why there's nothing where you expected something! So, of course, you have to look in the code for where you think you are putting values into those variables. If you look there, you may find something wrong or missing.
Re: Input from form is not going into mysql table
Posted: Thu Oct 07, 2010 1:21 am
by pkphp
If you notice your field name 'name '. Have a try to remove the empty space.
Re: Input from form is not going into mysql table
Posted: Thu Oct 07, 2010 9:10 am
by mikosiko
I get nothing on the next page..it is just blank. That means that the variables are not being filled up when in fact they should be
Not necessarily... a complete blank page can also means that you have a fatal error in your code (sometimes a missing ; or no closed ( or { could cause that blank page too) ... check your code for syntaxes problems and also add this two lines at the begging of your code to see if you trap some other error
Code: Select all
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
// rest of your code below
.....
Re: Input from form is not going into mysql table
Posted: Fri Oct 08, 2010 4:55 pm
by lilpenguin
I insertef those 2 lines and now know what the problem is is undefined index in all of my field names. Of course I defined tehm in html form in the other page. Here is the code in php that is causing the problem. I do suspect it is syntax, but my question is why I am receiving syntax error if I put $_POST["name"], as I think that is the correct way. But I dont get syntax error when I write it as it is below. Just undefined index error.
Code: Select all
$sql=mysql_query("INSERT INTO users (
name,
username,
password,
confirm,
email,
offer,
music
)
VALUES
(
$_POST[name],
$_POST[username],
$_POST[password],
$_POST[confirm],
$_POST[email],
$_POST[offer],
$_POST[music]
)");
if (!mysql_query($sql,$conn))
{
die('Error: ' . mysql_error());
}
echo "You have been added to the site.";
Re: Input from form is not going into mysql table
Posted: Fri Oct 08, 2010 8:26 pm
by califdon
What that error is telling you is that your HTML form from the other page is NOT creating the POST array values, so there are no indexes like 'name', 'username', etc.
So now you have to examine your other page, the HTML form. You apparently have a problem in the way you defined your form or the way it is being submitted. If you will post that here, we can help you find the error there.
This kind of web form processing can be tricky until you learn exactly how it operates. In this case, you are experiencing the bad results and errors in one script, but that's not where the problem is! It's in the other page.
Re: Input from form is not going into mysql table
Posted: Fri Oct 08, 2010 8:35 pm
by lilpenguin
Thank you. I have been looking further into it, and I gathered that much as well. I just now defined the variables in the other page as I'll post here but still get error. I'll keep looking at it but meanwhile here's the code.
[syntax="php]
<?
$name=$_POST["name"];
$username=$_POST["username"];
$password=$_POST["password"];
$confirm=$_POST["confirm"];
$email=$_POST["email"];
$offer=$_POST["offer"];
$music=$_POST["music"];
?>
<html>
<head>
<style type="text/css">
@import url("style.css");
</style>
</head>
<body>
<form name="registration" type="POST" action="register.php" >
Name: <input type="text" name="name">
<p><br>
Desired username:
<input type="text" name="username">
</p>
<p><br>
Password:
<input type="text" name="password">
</p>
<p><br>
Confirm Password: <input type="text" name="confirm">
</p>
<p><br>
Email: <input type="text" name="email">
</p>
<p><br>
Contribute one of the following:<br>
<input type="radio" name="offer" value="stay"> Place for musicians to stay <br>
<input type="radio" name="offer" value="play"> Place for musicians to play <br>
<input type="radio" name="offer" value="musician"> I play music </p>
<p><br>
Type of music played/hosted at venu
<select name="music">
<option>Alternative</option>
<option>Blues</option>
<option>Classical</option>
<option>Hiphop</option>
<option>Jazz</option>
<option>Latin</option>
<option>Reggae</option>
<option>Rock</option>
<option>World</option>
</select>
</p>
<p><br>
<input type="submit" name="Submit Form">
<br>
</p>
</form>
</body>
</html>
[/syntax]
Thanks!
Re: Input from form is not going into mysql table
Posted: Sat Oct 09, 2010 11:40 am
by califdon
No, you're missing the point of how HTML forms work. Here are some tutorials that should help you understand the process:
http://www.javascript-coder.com/html-fo ... l-p1.phtml
http://www.webcom.com/html/tutor/forms/start.shtml
You haven't shown us any of the code of your form, which is surely where the problem lies. The lines you showed us should not be in your form script at all. Those lines belong in the script that
handles the form data (the script that is indicated as the "action" in the form tag).
Re: Input from form is not going into mysql table
Posted: Mon Oct 11, 2010 12:06 pm
by lilpenguin
Right. The form itself is a very basic html form. (register.php is the handler in the action field)
Code: Select all
<form name="registration" type="POST" action="register.php" >
Name: <input type="text" name="name">
<p><br>
Desired username:
<input type="text" name="username">
</p>
<p><br>
Password:
<input type="text" name="password">
</p>
<p><br>
Confirm Password: <input type="text" name="confirm">
</p>
<p><br>
Email: <input type="text" name="email">
</p>
<p><br>
Contribute one of the following:<br>
<input type="radio" name="offer" value="stay"> Place for musicians to stay <br>
<input type="radio" name="offer" value="play"> Place for musicians to play <br>
<input type="radio" name="offer" value="musician"> I play music </p>
<p><br>
Type of music played/hosted at venu
<select name="music">
<option>Alternative</option>
<option>Blues</option>
<option>Classical</option>
<option>Hiphop</option>
<option>Jazz</option>
<option>Latin</option>
<option>Reggae</option>
<option>Rock</option>
<option>World</option>
</select>
</p>
<p><br>
<input type="submit" name="Submit Form">
<br>
</p>
</form>
Re: Input from form is not going into mysql table
Posted: Mon Oct 11, 2010 12:34 pm
by califdon
(Sorry, I guess you did post that code earlier, but I missed it.)
In your <select> form element, you have no option values. I haven't tested it, but I think that error is probably sufficient to cause the entire form not to be submitted, creating your problem. Your <select> element must look something like this:
Code: Select all
Type of music played/hosted at venu
<select name="music">
<option value='Alternative'>Alternative</option>
<option value='Blues'>Blues</option>
<option value='Classical'>Classical</option>
<option value='Hiphop'>Hiphop</option>
<option value='Jazz'>Jazz</option>
<option value='Latin'>Latin</option>
<option value='Reggae'>Reggae</option>
<option value='Rock'>Rock</option>
<option value='World'>World</option>
</select>
The words between <option> and </option> are not the
values of the options, only what is
displayed on the form. The two are often quite different.