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
https://forums.phpfreaks.com/topic/91920-inserting-into-database-help/
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();

?>

 

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.

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.