How to cancel action when detect same line in MySQL [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I need PHP code to detect is that line in MySQL database table.
Thanks

from my understanding of your question - in your php code you can do something like this
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$sqlSearch = "select everything from <table_name> where <column_name> = <value>;";
$result = mysqli_query($con,$sqlSearch );
if (mysqli_num_rows($result) > 0){
// do nothing
}
else{
$sqlInsert = "insert into <table_name> (column1,column2...) VALUES(value1,value2...);";
$result = mysqli_query($con,$sqlInsert);
}
?>
basically, you want to search the database to see if that row exist and if it does not you want to perform an insert query.

I hope this piece of code, helps you.
<?php
$variable1 = 1;
$query = "SELECT * FROM table_name WHERE column_name='$variable1'";
$result = $conn->query($query); //$conn is the connection variable
if($result->num_rows==0){
//insert into table if no duplicate tuple doesnot exist.
$insertquery = "INSERT INTO table_name (column_name) VALUES ('$variable1')";
$insertresult = $conn->query($insertquery);
}else{
//do something
}
?>

Related

My form only adds one user, I can add another one when i delete the previous user in my database. Anyone has any tips? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I know my code looks bad, but I just started using PHP. I can't fix the problem and I have been looking online for hours.
$sql = "INSERT INTO user (username, pass) VALUES ('$username', '$pass')";
$sql_check = "SELECT * FROM user WHERE username = '$username'";
$res = $conn->query($sql);
$res_check = $conn->query($sql_check);
if ($res_check->num_rows > 0) {
$melding = "Gebruikersnaam is al in gebruik";
} else if($res) {
$melding = "Gebruiker geregistreerd";
} else {
$melding = "Gebruiker niet geregistreerd";
}
you should check if your table already has an unique AND auto incrementing index!
Explanation:
If you add an entry the database will give it an unique id (counting from 1).
But if the field is not set as "auto incrementing" the index will always be zero and therefore it will fail to add another entry.
In phpmyadmin:
go to "structure" and edit the field ID. Check "AI" (auto increment) and make sure that it is used as primary key (key symbol).
Try This$conn is mysql connection variable$username user name variable $pass is user password
user_signup($conn,$username,$pass);
// Call function
function user_signup($conn,$user_name,$password)
{
$sql_check = "SELECT * FROM user WHERE username = '$user_name'";
$res_check = $conn->query($sql_check);
if ($res_check->num_rows > 0) {
echo "Username already exists!";
} else{
$sql = "INSERT INTO user (username, pass) VALUES ('$user_name', '$password')";
$conn->query($sql);
echo "insert data successfully";
// you can redirect another page
}
}

Auto Login after Registeration PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a problem in auto login to my-account page after registration. Please help, here is my code. I am trying to login user without any email confirmation.
<?php
#ob_start();session_start();
if($_SESSION['LOGIN_ID'] > 0)
header("location:myaccount.php");
$invalidcaptcha = 0;
if($_POST['register_submit'])
{
$ip = getIP();
$emailid = $_POST['emailid'];
$pwd = $_POST['pwd'];
$confirm = '1';
$blocked = '0';
$ccode = base64_encode($email);
$ures = mysql_query("select id from user where emailid='".mysql_real_escape_string($_POST['emailid'])."'");
if(mysql_num_rows($ures) > 0 )
{
header("location:login.php?emailexist=1");
}
else if($_POST['security_code']==$_SESSION['freecap_word_hash'])
{
mysql_query("insert into user set emailid='".mysql_real_escape_string($emailid)."',pwd='".md5($pwd)."',blocked='".$blocked."',confirm='".$confirm."',ccode='".$ccode."',reg_date=now(),reg_ip='".$ip."'");
$i_id = mysql_insert_id();
$res=mysql_query("SELECT id, emailid, pwd FROM users WHERE emailid='$emailid'");
$row=mysql_fetch_array($res);
$count = mysql_num_rows($res); // if uname/pass correct it returns must be 1 row
$_SESSION['LOGIN_ID'] = $row['userId'];
header("Location: myaccount.php");
}
}
?>
You are using userid here
$_SESSION['LOGIN_ID'] = $row['userId'];
And not using userId column in SELECT QUERY.
Other than to this issue, $i_id is equal to $row["userId"] than no need to use second SELECT QUERY.
In your case you just need to add userId column in this query:
SELECT id, emailid, pwd, userId FROM
But this query is extra as I mentioned.
This is another special point, stop using mysql_* its deprecated and closed in PHP 7, you can use mysqli_* or PDO.
It's better to use prepared statement, this will save your code with SQL Injection.

It should not insert values from php to database if these already exist [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Iam using a form in php to insert some values in my database. As you can see i retrieve from the form 4 values, id, name ,price and quantity. What i want to do is insert the values in the database table named catalog but only if id retrieved is not in the database before. The user cannot add id that exists.
I made an if that takes the results from select (*) count where id=$id. If the results is =0 then it should insert the values, else it should not and echo that id already exists. Unfortunately whatever i do, it keeps echoing that id already exists, and doesnt inserts the values in the catalog.
Any help and ideas would be appreciated :)
<?php
include("mysql.php");
session_start();
$link=mysql_connect($host,$user,$password);
mysql_select_db($database);
echo "<center>";
$id=$_POST['id'];
$name=$_POST['name'];
$price=$_POST['price'];
$quantity=$_POST['quantity'];
$sql="select count(*) from catalog";
$result=mysql_query($sql);
if(!$id || !$name || !$price || !$quantity)
die("You should write all the fields in form");
if ($sql=0)
{
$sql="insert into catalog values ('$id','$name','$price','$quantity')";
}
else
echo "id exists";
if(mysql_query($sql))
echo "Insert Successful<br><br>";
else
echo "<br>Insert NOT Successful<br><br><br>";
?>
First and foremost, why not just set a unique constraint on the columns in mysql if you don't want something existing to be inserted? That's an overall better solution to a problem like that in your title.
Secondly, in your code you are saying if $sql can be set to 0, not if $sql == 0 in the statement if ($sql=0). This will always return true because you are able to set $sql to 0.
Thirdly, you don't seem to understand what $result is what contains the result of your query, or what $result is.
I think you need to read more tutorials and get a better understanding of what you're doing.
Well couldnt do it as with that code but thanks for the help everybody.
Tried this and worked.
<?php
session_start();
$linu=mysql_connect("localhost", "root", "");
mysql_select_db("eshop");
$id=$_POST['id'];
$name=$_POST['name'];
$price=$_POST['price'];
$quantity=$_POST['quantity'];
$sql="select * from catalog where id='$id'";
$result=mysql_query($sql);
$N=mysql_num_rows($result);
if(!$id || !$name || !$price || !$quantity)
die("You should write all the fields in form");
if($N==1)
echo "id exists";
else
$sql2="insert into catalog values ('$id','$name','$price','$quantity')";
if(mysql_query($sql2))
{
echo "Insert Successful<br><br>";
}
else
{
echo "<br>Insert NOT Successful<br><br><br>";
}

PHP/SQL connection script [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
someone please help me to set mysql database connection variable on below php scripts but I couldn't fixed it.
$sql = "SELECT DISTINCT r.itemID, r2.ratingValue - r.ratingValue
as rating_difference
FROM rating r, rating r2
WHERE r.userID=$userID AND
r2.itemID=$itemID AND
r2.userID=$userID;";
$db_result = mysql_query($sql, $connection);
$num_rows = mysql_num_rows($db_result);
while ($row = mysql_fetch_assoc($db_result)) {
$other_itemID = $row["itemID"];
$rating_difference = $row["rating_difference"];
if (mysql_num_rows(mysql_query("SELECT itemID1
FROM dev WHERE itemID1=$itemID AND itemID2=$other_itemID",
$connection)) > 0) {
$sql = "UPDATE dev SET count=count+1,
sum=sum+$rating_difference WHERE itemID1=$itemID
AND itemID2=$other_itemID";
mysql_query($sql, $connection);
if ($itemID != $other_itemID) {
$sql = "UPDATE dev SET count=count+1,
sum=sum-$rating_difference
WHERE (itemID1=$other_itemID AND itemID2=$itemID)";
mysql_query($sql, $connection);
}
}
You didn't connect to any database use some thing like this
$link = mysql_connect('dbhost', 'dbusername', 'dbpassword');
$connection = mysql_select_db('tablename', $link);

MySQL table does not update [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is my code
I have 2 table
booking // create to keep the booking from member
userinfo //keep username of member
<?php
$ta = $_POST['table'];
$day=$_POST['date'];
$usern = $_SESSION['username'];
$con = mysql_connect("localhost","root","password") or die ("can't connect to host");
mysql_select_db("register",$con) or die("can't connect to database");
$sql = "select * from userinfo where username = '$usern' ";
$rs = mysql_query($sql);
$row = mysql_num_rows($rs);
if($row == 0)
{ mysql_query("insert into booking(username) values '".$usern. "' ");
mysql_db_query("register");
} mysql_close();
?>
i don't know why it doesn't update.
You're missing your parenthesis in your INSERT statement:
"insert into booking(username) values '".$usern. "' "
should be
"insert into booking(username) values ('".$usern. "') "
First things first: mysql is becoming unsupported. Use Mysqli instead. It's not much different...
Then, the issue you have is to leave out the "_db". It's just mysql_query(your request). Plus, the second statement doesn't need to exist.
Need error handling? Include (before ;) "or die (mysql_error())

Categories