Reputation: 1
Could you help me with my script.
I have made an phpmysql script in which username and email is taken from a form and run it through the database. If the username and email exists in the database it will show 'already exist' other wise it will add to the database.
This is my script.The script is working (not showing any errors) but in the if else statement there is some problem (I think a syntax problem), it only shows already member exist
even if it is not in database. I am notable to identify the problem. I am a newbe in phpmysql
if(isset($_POST['submit'])) {
$name=mysql_real_escape_string($_GET['name']);
$tes=mysql_real_escape_string($tes);
print $name;
print $tes;
$sel=mysql_query("SELECT * FROM member WHERE email='$tes' AND usrename='$name'");
if(!isset($sel)) {
echo('There is already a member');
}
else
{
//insert
$sql = "INSERT INTO member SET username='$name',email='$tes', date=CURDATE()";
if (@mysql_query($sql)) {
echo('<p>You have been added to database.</p>');
}
}
}
Upvotes: 0
Views: 160
Reputation: 2561
you need to add some code..
<?php
$sel=mysql_query("SELECT * FROM member WHERE email='".$tes."' AND usrename="'.$name.'");
$data = mysql_fetch_array($sel)
if($data[0]['email']!=''){
echo 'not in database..';
}
else{
echo 'in database....';
}
?>
Upvotes: 1
Reputation: 4547
<?php
$usename = $_POST['username'];
$pass = $_POST['password'];
$rowcount = mysqli_num_rows($result);
if($rowcount > 0)
{
echo "Usename already exits";
}
else
{
$sql = mysql_query('INSERT INTO `table_name`');
echo (!empty($sql)) ? "User added." : "Error occured.";
}
?>
Upvotes: 0
Reputation: 872
try this solution
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$mysqli = new mysqli("localhost", "root", "", "articles");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Select queries return a resultset */
$con = 1;
$sql = sprintf("SELECT * FROM users WHERE user_name='%s'", mysql_real_escape_string($user));
if ($result = $mysqli->query($sql)) {
printf("Select returned %d rows.\n", $result->num_rows);
if($result->num_rows == 0) {
//TODO :: insert new record
}
else {
//TODO:: USER already exists
}
/* free result set */
$result->close();
}
$mysqli->close();
?>
Upvotes: 0
Reputation: 103
On your mysql_query line there is a spelling error: AND usrename should be AND username
Upvotes: 2
Reputation: 6583
instead of
if(!isset($sel))
you can to use
if(mysql_num_rows($sel) > 0)
check mysql_num_rows
Upvotes: 2
Reputation: 400932
The following portion of code is not doing what you think it is :
$sel=mysql_query("SELECT * FROM member WHERE email='$tes' AND usrename='$name'");
if(!isset($sel)) {
echo('There is already a member');
}
mysql_query()
, as long as the query doesn't fail (because of an error in the SQL), will return a resource allowing you to access the resultset.
And even if the query fails, it'll return false -- with means the variable will still be set.
What you probably want is to try fetching some data from the $set resource, to see if your expected (or not) data is there -- see for example mysql_fetch_array()
.
And if you only want to check if a user exists, no need for a select *
query -- just use a select count(*)
, to see how many users correspond to your criteria, without loading their data.
Upvotes: 1