Jump to content

Inserting into database help


artweb

Recommended Posts

I have a mysql database set up. where I manual go in and put the usernames and Passwords of customer that register with me though my email. Then when they receive and email from me telling then they can login. I also have another table set up to record their login info, company name and usernmane and password. The problem is I want this to only work if the user is in the first table. But  even if the user doesn't exist in my first table it records everything in my second table. So I need something like if login successful insert into second table. But only if successful. Please help ???

 

code bellow:

 

 

 

<?php

ob_start();

$host=""; // Host name

$username=""; // Mysql username

$password=""; // Mysql password

$db_name=""; // Database name

$tbl_name="members"; // Table name

 

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// Define $myusername and $mypassword

$myusername=$_POST['myusername'];

$mypassword=$_POST['mypassword'];

 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);

 

 

//mysql_num_row is counting table row

$count=mysql_num_rows($result);

//if result match  $myusername  and mypassword  table row must be 1 row

 

if($count==1){

session_register("myusername");

header("location:happy.php");

}

else {

echo "Wrong Username or Password";

}

if($count==1){

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("test_mysql")or die("cannot select DB");

}

$sql="INSERT INTO test_mysql(companyname, myusername, mypassword)VALUES('$companyname', '$myusername', '$mypassword')";

$result=mysql_query($sql);

ob_end_flush();

?>

 

Link to comment
Share on other sites

Thanks for your help :)

Ok, I changed it like this, code below, and now nothing get recorded into my second table. I still don't know what I'm doing. Please help more.

 

<?php

 

 

$tbl_name="members"; // Table name

 

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// Define $myusername and $mypassword

$myusername=$_POST['myusername'];

$mypassword=$_POST['mypassword'];

 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);

 

 

//mysql_num_row is counting table row

$count=mysql_num_rows($result);

//if result match  $myusername  and mypassword  table row must be 1 row

 

if($count==1){

session_register("myusername");

header("location:happy.php");

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("test_mysql")or die("cannot select DB");

 

$sql="INSERT INTO test_mysql(companyname, myusername, mypassword)VALUES('$companyname', '$myusername', '$mypassword')";

$result=mysql_query($sql);

}

else {

echo "Wrong Username or Password";

}

 

ob_end_flush();

?>

 

Link to comment
Share on other sites

If I don't connect to the second table how will I be able to insert into.

 

The first table is for storing username and passwords so when user fills out the form to login, it checks the database for that username and password if it finds it. It will let them in to the protected part. If not  it makes them try again.

 

The second is for recording when the user logs in. It records even if the user has to try again.

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.