AnAmericanGunner Posted April 11, 2010 Share Posted April 11, 2010 My register page is this: <? session_name('usersession'); session_start(); include('includes/database.php'); include('inclues/header.php'); ?> <? $username = $_POST['user']; $password = md5($_POST['pass']); $result = mysql_query("SELECT * FROM user WHERE username='$username'") or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows !== 0){ echo "Username already taken, please choose another one!"; } else{ mysql_query("INSERT INTO user (username, passwd) VALUES('$username', '$password' ) ") or die(mysql_error()); echo "Welcome to ER, you may now login!"; } ?> <a href="index.php">Return Home</a> </body> </html> My question is this: Can I wrote another INSERT INTO query to insert the username into a table called BALANCE? The username would be going into the column 'description' and '5' would have to go into a column called 'credits'. Link to comment https://forums.phpfreaks.com/topic/198223-insert-function-inserting-into-two-tables-with-my-code/ Share on other sites More sharing options...
Jax2 Posted April 11, 2010 Share Posted April 11, 2010 You can put two queries in a row if you like ... $sql=" first query, put X into table Y"; $result=mysql_query($sql, $db); $sql=" second query, put A into table B"; $result=mysql_query($sql, $db); ... or however you write your queries ... Link to comment https://forums.phpfreaks.com/topic/198223-insert-function-inserting-into-two-tables-with-my-code/#findComment-1040058 Share on other sites More sharing options...
the182guy Posted April 11, 2010 Share Posted April 11, 2010 Of course you can use another INSERT INTO query. Example <?php session_name('usersession'); session_start(); include('includes/database.php'); include('inclues/header.php'); ?> <? $username = $_POST['user']; $password = md5($_POST['pass']); $result = mysql_query("SELECT * FROM user WHERE username='$username'") or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows !== 0){ echo "Username already taken, please choose another one!"; } else{ $username = mysql_real_escape_string($username); // important! see SQL Injection $resultUser = mysql_query("INSERT INTO user (username, passwd) VALUES('$username', '$password' ) ") or die(mysql_error()); // now insert into the user_credits table $resultCredits = mysql_query("INSERT INTO user_credits (description, credits) VALUES ('$username', 5)"); echo "Welcome to ER, you may now login!"; } ?> <a href="index.php">Return Home</a> </body> </html> Link to comment https://forums.phpfreaks.com/topic/198223-insert-function-inserting-into-two-tables-with-my-code/#findComment-1040059 Share on other sites More sharing options...
AnAmericanGunner Posted April 11, 2010 Author Share Posted April 11, 2010 Thanks for the replies! I shall try them out soon. Link to comment https://forums.phpfreaks.com/topic/198223-insert-function-inserting-into-two-tables-with-my-code/#findComment-1040065 Share on other sites More sharing options...
AnAmericanGunner Posted April 11, 2010 Author Share Posted April 11, 2010 Thanks the182guy. Your code worked perfectly Link to comment https://forums.phpfreaks.com/topic/198223-insert-function-inserting-into-two-tables-with-my-code/#findComment-1040092 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.