Jump to content

inserting data into database with use of variables -only inserts into 1 table


Recommended Posts

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>";

 

} 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()");


 

 

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

<?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()");
?>

 

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>

 

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?

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.