Jump to content

[Insert Function] Inserting into two tables with my code


AnAmericanGunner

Recommended Posts

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'.

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 ...

 

 

 

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.