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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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