Hi,

I've been racking my brain for the last few days to figure out what is wrong with my PHP coding. I've managed to get the default value from my combo box into my database, but when I change the option it is breaking on me.

I have reduced my html to just to combo box for you, please can you help me?

Here is my HTML:


<html>

<body>

<form action="Signup.php" method="post">
Username: <input type="text" name="Username" /> <br/>
Password: <input type="text" name="Password" /> <br/>

Secret Question: <select name="Secret_Question">
<option value="What is your mother's maiden name?">What is your mother's maiden name?</option>
<option value="What is your place of birth?">What is your place of birth?</option>
<option value="What is your favourite food?"selected="selected">What is your favourite food?</option>
<option value="What is your favourite pet's name?">What is your favourite pet's name?</option>
</select> <br/

<input type="submit" value="Submit" />
</form>

</body>

</html>

I have not bothered with any validation yet, I just want to learn this basic lesson first.

Here is my PHP:

//DECLARING VARIABLES
$host = "I have my IP address in here"; // Host
$dbuser="web41-admin-2"; // Mysql username
$dbpassword="I have my password here"; // Mysql password
$db_name="web41-admin-2"; // Database name
$tbl_name="Users"; // Table name

$Username = $_POST;
$Password = $_POST;
$Secret_Question = $_POST;
$Secret_Answer = $_POST;
$first_name = $_POST;
$surname = $_POST;
$email = $_POST;

//ESTABLISHING CONNECTION
$con = mysql_connect("$host","$dbuser","$dbpassword");
if (!$con) //IF CONNECTION FAILS
die('Connection failed: ' . mysql_error()); //OUTPUTS ERROR MESSAGE
Else
Echo "It worked"; //INFORMS OF SUCCESS

mysql_select_db($db_name, $con); // ALL FUNCTIONAL TO THIS POINT

$query = "INSERT into Users values
('".$Username."','".$Password."','".$Secret_Question."','".$Secret_Answer."','".$first_name."','".$surname."','".$email."')";
$result = mysql_query($query);
if ($result)
echo mysql_affected_rows(). "user added";
?>

Also any feedback about my coding style will be greatly appreciated as I am a complete beginner.

Recommended Answers

All 3 Replies

Please show the "CREATE TABLE Users" statement.
And for debugging change

$result = mysql_query($query);

to

$result = mysql_query($query) or die(mysql_error());

When you say breaking, can you please explain as to what is happening? Are you not getting the values into the DB? or is something else wrong.

I have not tried out your code yet, but there are a few things that you should change to check this.

1. When you are testing out a page use GET instead of POST. It makes it easier to check since the query string comes up in the status bar. This will help you make sure what you are transferring is correct. But also make sure you change your GET back into POST when you go into production.

2. When you do an INSERT statement, it is always preferable to specify the column names so that you can make sure your data, goes into the specific columns in your table and does not get mixed up. Use INSERT INTO Users (username,password,secret_question,secret_answer,first_name,surname,email) VALUES '$Username','$Password','$Secret_Question','$Secret_Answer','$first_name','$surname','$email')"; 3. Since you are using specific questions, you can code your questions so that the database does not build up in size for e.g.
1 -> What is your mother's maiden name?
2 -> What is your place of birth? etc.

and make the select statement as <option value="1">What is your mother's maiden name?</option> After looking at this, I think the problem as to why you code breaks is because of the ? in the option value. Check without it and see.

Everything else looks ok. Let me know if it works after checking the above.

Thanks for your help, sorted it out now! Much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.