yddib Posted September 10, 2008 Share Posted September 10, 2008 Hi, I am having a problem assigining an auto number generated in one table to two other tables. When people register on the site, there is an autonumber generated in the database. I am trying to insert this into 2 other fields in different tables. When I tried this is stopped inserting anything to the database. Please help! Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/ Share on other sites More sharing options...
Mchl Posted September 10, 2008 Share Posted September 10, 2008 Show us some code. Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638259 Share on other sites More sharing options...
yddib Posted September 10, 2008 Author Share Posted September 10, 2008 There is a html form - the action is this page: <?php session_start(); $id = $_SESSION['id']; ?> <html> <head> <title>4grads - Contact Us</title> <link rel="stylesheet" href="style1.css" type="text/css" /> <style type="text/css"> form.pos {position:absolute; left: 250px} </style> <meta HTTP-EQUIV="REFRESH" content="0; url=http://www.4grads.net"> </head> <body> <div id="sidebar"> <a href="index.html" /><img src="translogo.jpg" alt="4Grads Logo" /></a><br /><br /><br /><br /> <div id="menu" style="width: 170; height: 220"> </div> </div> <div id="content"> <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" background="bannercreator-nu2.gif" height="55"> <tr> <td width="5%" height="55" align="center"> <td width="40%" height="55" align="center"> <font color="#FFFFFF" size="3">Registration Successful</font></td> <td width="15%" height="55" align="center"></td> <td width="20%" height="55" align="center"> </td> <td width="20%" height="55" align="center"> <font color="#FFFFFF" size="3"></font></td> <td width="20%" height="55" align="center"> <a href="help.html"><font color="#FFFFFF" size="3">Help </font></a></td> </tr> </table> <?php mysql_connect("host", "user", "pass") or die(mysql_error()); //connects to the blacknight server mysql_select_db("db")or die(mysql_error()); $fname=$_POST['myname']; $lname=$_POST['myname1']; $email=$_POST['myEmail']; $pass=$_POST['pass']; $gender=$_POST['gender']; $birthm=$_POST['birthm']; $birthd=$_POST['birthd']; $birthy=$_POST['birthy']; $address1=$_POST['address1']; $county=$_POST['county']; $country=$_POST['country']; $phone=$_POST['phone']; $location=$_POST['location']; $course=$_POST['course']; $occupation=$_POST['occupation']; mysql_query("INSERT INTO gradinfo (f_name,l_name,email,pass,gender,birthm,birthd,birthy,address1,county,country,telephone,location) VALUES ('$fname','$lname','$email','$pass','$gender','$birthm','$birthd','$birthy','$address1','$county','$country','$phone','$location')");//Set SQL mysql_query("INSERT INTO gradcoll (u_course) VALUES ('$course')"); mysql_query("INSERT INTO gradcoll (u_id) VALUES ('$id')"); mysql_query("INSERT INTO gradwork (occupation) VALUES ('$occupation')"); mysql_query("INSERT INTO gradwork (u_id) VALUES ('$id')"); echo '<br /><br />Congratulations!! You have successfully joined <i>4Grads</i>'; echo '<br /><br />Please Login <a href="index.html">HERE</a>' ?> </body> </html> This bit of code made it stop entering any info into the database <?php session_start(); $id = $_SESSION['id']; ?> and mysql_query("INSERT INTO gradcoll (u_id) VALUES ('$id')"); Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638264 Share on other sites More sharing options...
BlueSkyIS Posted September 10, 2008 Share Posted September 10, 2008 life is better when you check for SQL errors and check to make sure the SQL looks like you expect. $sql = "INSERT INTO gradcoll (u_id) VALUES ('$id')"; mysql_query($sql) or die(mysql_error()); echo "sql worked: $sql<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638266 Share on other sites More sharing options...
yddib Posted September 10, 2008 Author Share Posted September 10, 2008 Unknown MySQL server host 'host' (11004) I got an error Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638271 Share on other sites More sharing options...
BlueSkyIS Posted September 10, 2008 Share Posted September 10, 2008 i highly doubt that your MySQL server is located at 'host' with user name 'user' and password 'pass'. do you know the actual credentials to connect to your MySQL server? mysql_connect("host", "user", "pass"); // possible, but not likely! Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638276 Share on other sites More sharing options...
yddib Posted September 10, 2008 Author Share Posted September 10, 2008 I am not going to put up my username and password!! New error (sort of!) sql work: INSERT INTO gradcoll (u_id) VALUES ('') Means it is blank doesn't it? All values from the form go into the database but the user id isn't. Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638283 Share on other sites More sharing options...
Mchl Posted September 10, 2008 Share Posted September 10, 2008 <?php session_start(); print_r($_SESSION); //add this to see what's inside $_SESSION, it seems that id isn't there... $id = $_SESSION['id']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638287 Share on other sites More sharing options...
yddib Posted September 10, 2008 Author Share Posted September 10, 2008 Yeah I got the same sql work: INSERT INTO gradcoll (u_id) VALUES ('') The id is created in the database as an autonumber in the gradinfo table....I need the same number to go into gradcoll so that they're linked to the same person Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638291 Share on other sites More sharing options...
BlueSkyIS Posted September 10, 2008 Share Posted September 10, 2008 after you insert into gradinfo, you can get the new record's id using: $new_id = mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638294 Share on other sites More sharing options...
yddib Posted September 10, 2008 Author Share Posted September 10, 2008 after you insert into gradinfo, you can get the new record's id using: Code: $new_id = mysql_insert_id(); I think this could work but could you describe how a bit more? Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638357 Share on other sites More sharing options...
yddib Posted September 10, 2008 Author Share Posted September 10, 2008 I'd really appreciate any help cos my whole registration process is upset a bit! Please help! Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638380 Share on other sites More sharing options...
yddib Posted September 10, 2008 Author Share Posted September 10, 2008 $id = mysql_insert_id(); Inserted after first insert statement...... It does what I want it to do as in it recognises the user ID and adds it to the other tables in the database BUT (and it's a big but!) it doesn't add the 'course' or 'occupation' to the same line.....i.e. the reason I need the user ID.... :'( Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638395 Share on other sites More sharing options...
yddib Posted September 10, 2008 Author Share Posted September 10, 2008 anyone? ??? Please?!! Head being banged off keyboard!! Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638415 Share on other sites More sharing options...
sasa Posted September 10, 2008 Share Posted September 10, 2008 merge this two line mysql_query("INSERT INTO gradcoll (u_course) VALUES ('$course')"); mysql_query("INSERT INTO gradcoll (u_id) VALUES ('$id')"); to mysql_query("INSERT INTO gradcoll (u_course, u_id) VALUES ('$course', '$id')"); and next two to Quote Link to comment https://forums.phpfreaks.com/topic/123594-solved-autonumber-into-3-tables/#findComment-638448 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.