witchy478 Posted June 20, 2012 Share Posted June 20, 2012 Hi I would like to have the user enter his data into a form and then that form get directed to another page where the user has to enter 3 friends emails. My problem is that when when the user enters the data the email_id which is the foreign key does not go in the database. This is the insert.php <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $db_table="emails"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); ?> <?php require_once("validation.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Sign up</title> <link rel="stylesheet" href="css/general.css" type="text/css" media="screen" /> </head> <body> <div id="container"> <h1>Sign up process</h1> <form method="post" id="customForm" action="invite.php"> <div> Name <input id="name" name="name" type="text" /> </div> <div> E-mail <input id="email" name="email" type="text" /> </div> <div> Password <input id="pass1" name="password" type="password" /> <span id="pass1Info">At least 5 characters</span> </div> <div> Confirm Password <input id="pass2" name="pass2" type="password" /> <span id="pass2Info">Confirm password</span> </div> <div> <input id="send" type="submit" value="Send" /> </div> </form> </div> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="validation.js"></script> </body> </html> <?php // Get values from form $name=$_POST['name']; $email=$_POST['email']; $password=$_POST['password']; // Insert data into mysql $sql="INSERT INTO $db_table(name, email, password)VALUES('$name', '$email', 'password')"; $result=mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result){ echo "Successful"; echo "<BR>"; echo "<a href='insert.php'>Back to main page</a>"; } else { echo "ERROR"; } // close connection mysql_close(); ?> and this is the invite.php where the user enters his friends emails <?php $hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost $db_user = ""; // change to your database password $db_password = ""; // change to your database password $database = "test"; // provide your database name $db_table = "friend_emails"; // leave this as is # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($database,$db); ?> <html> <head> <title>Invite your friends</title> </head> <body> <?php if (isset($_REQUEST['Submit'])) { # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE $sql = "INSERT INTO $db_table(friend1, friend2, friend3) values (".$_REQUEST['email_id'].", '".mysql_real_escape_string(stripslashes($_REQUEST['friend1']))."','".mysql_real_escape_string(stripslashes($_REQUEST['friend2']))."','".mysql_real_escape_string(stripslashes($_REQUEST['friend3']))."')"; if($result = mysql_query($sql ,$db)) { echo '<h1>Thank you</h1>Your information has been entered into our database<br>'; } else { echo "ERROR: ".mysql_error(); } } else { ?> <h1>Invite your friends</h1> <form method="post" action=""> Email:<br> <input type="text" name="friend1"> <br> Email: <br> <input type="text" name="friend2"> <br> Email:<br> <input type="text" name="friend3"> <br> <br> <input type="submit" name="Submit" value="Submit"> </form> <?php } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/264492-insert-into-problem/ Share on other sites More sharing options...
boompa Posted June 20, 2012 Share Posted June 20, 2012 Look at your query: $sql = "INSERT INTO $db_table(friend1, friend2, friend3) values (".$_REQUEST['email_id'].", '".mysql_real_escape_string(stripslashes($_REQUEST['friend1']))."','".mysql_real_escape_string(stripslashes($_REQUEST['friend2']))."','".mysql_real_escape_string(stripslashes($_REQUEST['friend3']))."')"; You are trying to insert four values, but you're telling MySQL you're only going to insert three. Where is email_id set in the request? I don't see it. Quote Link to comment https://forums.phpfreaks.com/topic/264492-insert-into-problem/#findComment-1355434 Share on other sites More sharing options...
witchy478 Posted June 20, 2012 Author Share Posted June 20, 2012 I have entered the email_id in the insert statement $sql = "INSERT INTO $db_table(email_id, friend1, friend2, friend3) values (".$_REQUEST['email_id'].", '".mysql_real_escape_string(stripslashes($_REQUEST['friend1']))."','".mysql_real_escape_string(stripslashes($_REQUEST['friend2']))."','".mysql_real_escape_string(stripslashes($_REQUEST['friend3']))."')"; and I get ER \ROR: 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 ' '[email protected]','[email protected]','[email protected]')' at line 1 as well as Notice: Undefined index: email_id in C:\wamp\www\test\invite.php on line 25 Quote Link to comment https://forums.phpfreaks.com/topic/264492-insert-into-problem/#findComment-1355440 Share on other sites More sharing options...
boompa Posted June 20, 2012 Share Posted June 20, 2012 As stated in my previous post: Where is email_id set in the request? I don't see it. If the ID in the table is an autoincremented primary key, then you don't insert it at all. You let the database generate it and just insert the rest of the row's data. Quote Link to comment https://forums.phpfreaks.com/topic/264492-insert-into-problem/#findComment-1355442 Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2012 Share Posted June 20, 2012 Notice: Undefined index: email_id in C:\wamp\www\test\invite.php on line 25 Obviously, $_REQUEST['email_id'] doesn't have a value. You need to validate things before trying to use them. Quote Link to comment https://forums.phpfreaks.com/topic/264492-insert-into-problem/#findComment-1355475 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.