938660 Posted October 13, 2007 Share Posted October 13, 2007 hi basically im not good with php yet. this code insters data correctly into the 1st table but not the second. I bet a simple modification is required but i cant figure it out - ive tried many things: $sql = "SELECT * FROM users WHERE username = '$username'"; // Database checks $result = mysql_query($sql) or die(mysql_error()); $num=mysql_num_rows($result); if ($num){ $errors .="That username is already in use<Br>";} if (!$password){ $errors .="You must input a password!<Br>";} if (!$email){ $errors .="You must provide an email address<br>";} if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { $errors .= "Please provide us with a valid email address<br>";} if ($errors){ echo "<b>The following errors occurred:</b><br><br>"; echo $errors; } else { // insert a new user to the database $sql = "INSERT INTO users (id, username, password, name, email) VALUES ('', '$username', '$password', '$name', '$email')"; "INSERT INTO user1 (username, password) VALUES ('$username', '$password')"; $result = mysql_query($sql); echo "You have now signed up. <a href = \"login1.php\">Please login</a>"; Link to comment https://forums.phpfreaks.com/topic/73101-inserting-data-into-database-with-use-of-variables-only-inserts-into-1-table/ Share on other sites More sharing options...
redarrow Posted October 13, 2007 Share Posted October 13, 2007 } else { $username=addslashes($_post['username']); $password=addslashes($_post['password']); $name=addslashes($_post['name']); $email=addslashes($_post['email']); $sql = "INSERT INTO `users` (`id`, `username`, `password`, `name`, `email`) VALUES ('', '$username', '$password', '$name', '$email')"; "INSERT INTO user1 (username, password) VALUES ('$username', '$password')"; $result = mysql_query($sql)or die ("mysql_error()"); Link to comment https://forums.phpfreaks.com/topic/73101-inserting-data-into-database-with-use-of-variables-only-inserts-into-1-table/#findComment-368674 Share on other sites More sharing options...
938660 Posted October 13, 2007 Author Share Posted October 13, 2007 i already have the following in my code: $username=$_POST["username"]; $password=$_POST["password"]; $email=$_POST["email"]; $name=$_POST["name"]; this still doesnt add the data to both tables, only the 'users' table Link to comment https://forums.phpfreaks.com/topic/73101-inserting-data-into-database-with-use-of-variables-only-inserts-into-1-table/#findComment-368676 Share on other sites More sharing options...
esukf Posted October 13, 2007 Share Posted October 13, 2007 <?php $sql = "INSERT INTO `users` (`id`, `username`, `password`, `name`, `email`) VALUES ('', '$username', '$password', '$name', '$email')"; $sql1 = "INSERT INTO user1 (username, password) VALUES ('$username', '$password')"; $result = mysql_query($sql)or die ("mysql_error()"); $result1 = mysql_query($sql1)or die ("mysql_error()"); ?> Or are you trying to have 2 insert statement separated by ;? <?php $sql = "INSERT INTO `users` (`id`, `username`, `password`, `name`, `email`) VALUES ('', '$username', '$password', '$name', '$email'); INSERT INTO user1 (username, password) VALUES ('$username', '$password')"; $result = mysql_query($sql)or die ("mysql_error()"); ?> Link to comment https://forums.phpfreaks.com/topic/73101-inserting-data-into-database-with-use-of-variables-only-inserts-into-1-table/#findComment-368690 Share on other sites More sharing options...
938660 Posted October 13, 2007 Author Share Posted October 13, 2007 To be honest, I just need the data to be insreted to both tables. I dont care which way it is done. It inserts into 'users' but not user1, im sure it just needs to be tweaked.. have i broken any rules? is the syntax correct? etc...is it the way how ive used the same variables to insert data into both tables? Please help ! lol here it is in full <? // Check if they are already logged in if ($_SESSION['logged_in']==1){ echo "You are already logged in!"; // Check that the form was submitted and check the user } else if (isset($_POST['submit'])){ // do user database checking $username=addslashes($_post['username']); $password=addslashes($_post['password']); $name=addslashes($_post['name']); $email=addslashes($_post['email']); $sql = "SELECT * FROM users WHERE username = '$username'"; // Database checks $result = mysql_query($sql) or die(mysql_error()); $num=mysql_num_rows($result); if ($num){ $errors .="That username is already in use<Br>";} if (!$password){ $errors .="You must input a password!<Br>";} if (!$email){ $errors .="You must provide an email address<br>";} if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { $errors .= "Please provide us with a valid email address<br>";} if ($errors){ echo "<b>The following errors occurred:</b><br><br>"; echo $errors; } else { // insert a new user to the database $sql = "INSERT INTO `users` (`id`, `username`, `password`, `name`, `email`) VALUES ('', '$username', '$password', '$name', '$email')"; "INSERT INTO user1 (username, password) VALUES ('$username', '$password')"; $result = mysql_query($sql)or die ("mysql_error()"); echo "You have now signed up. <a href = \"login.php\">Please login</a>"; } } else { ?> <form method="post" action="<? echo $PHP_SELF?>"> <table border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td>Username</td> <td><input name="username" type="text" id="username" /></td> </tr> <tr> <td>Password</td> <td><input name="password" type="password" id="password" /></td> </tr> <tr> <td>Your name </td> <td><input name="name" type="text" id="name" /></td> </tr> <tr> <td>email address </td> <td><input name="email" type="text" id="email" /></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><input name="submit" type="submit" id="submit" value="Register" /></td> </tr> </table> </form> <? } ?> </td> </tr> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/73101-inserting-data-into-database-with-use-of-variables-only-inserts-into-1-table/#findComment-368712 Share on other sites More sharing options...
esukf Posted October 13, 2007 Share Posted October 13, 2007 Have you tried replacing your code:- // insert a new user to the database $sql = "INSERT INTO users (id, username, password, name, email) VALUES ('', '$username', '$password', '$name', '$email')"; "INSERT INTO user1 (username, password) VALUES ('$username', '$password')"; $result = mysql_query($sql); with what one of the suggestions I gave before? Are you getting any errors with the insert? Link to comment https://forums.phpfreaks.com/topic/73101-inserting-data-into-database-with-use-of-variables-only-inserts-into-1-table/#findComment-368738 Share on other sites More sharing options...
938660 Posted October 13, 2007 Author Share Posted October 13, 2007 hi esukf - i tried both of ure suggested ones, same problem, data only gets inserted into 'users'. no erros esukf , i use XAMP, I JST MODIFY SCRIPT IN NOTEPAD and try again cheers Link to comment https://forums.phpfreaks.com/topic/73101-inserting-data-into-database-with-use-of-variables-only-inserts-into-1-table/#findComment-368755 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.