Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Please im trying to insert data from a table to another. the issue is that i find duplicate records in database when applying the code below:
<?php
include ('lib/connexion.php');
$id_article = $_GET['num'];
$requete = "Select * from products where product_id=$id_article";
$resultats = mysql_query ($requete);
if($resultats === FALSE) {
die(mysql_error()); // TODO: better error handling
}
?>
<html>
<head>
<title>APP crud</title>
</head>
<body>
<?php
while ($ligne =mysql_fetch_array ($resultats)){
$sql2 ="INSERT INTO panier (product_title, description, prix)
VALUES ('".$ligne[1]."','".$ligne[2]."','".$ligne[3]."' ) ";
mysql_query ($sql2) or die ('Erreur : ' .mysql_error());
$resultats2 = mysql_query ($sql2);
if($resultats2 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
header('Location: panier.php');
?>
Supprimer
Modifier
Ajouter
Retour
<?php } ?>
</body>
<html>
Can someone tell me why this happens? thanks.
Making this as a community wiki (I've nothing to gain here, or wanting to gain) and pulled from comments to close this with:
Because, you're doing this twice: mysql_query ($sql2)
"Remove this line mysql_query ($sql2) or die ('Erreur : ' .mysql_error()); – devpro"
and
"Mysql_ is deprecated use mysqli_* or PDO – devpro"*
and
"you could also ALTER your column(s) as UNIQUE. That will guarantee that no duplicates ever gets inserted."
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to insert data in SQL table, but it's duplicating the values:
$star1 = trim($_GET['star1']);
$star2 = trim($_GET['star2']);
$star3 = trim($_GET['star3']);
$star4 = trim($_GET['star4']);
$star5 = trim($_GET['star5']);
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
die("Desculpe.. Falha ao salvar ");
}
$sql = "INSERT INTO classificacoes (nossa_empresa, produtos_oferecidos,atendimento, colaboradores, indicaria) VALUES ('$star1','$star2','$star3','$star4', '$star5')";
$conn->query($sql);
if ($conn->query($sql) != TRUE){
echo "Erro ao gravar";
}
else{
echo "Gravou";
}
mysqli_close($conn);
That is: The code insert two identincal values in the table.
What's wrong?
The problem is you're running the query twice:
$conn->query($sql);
if ($conn->query($sql) != TRUE){
The if line is also running the query, so you should remove the line above the if. Your code should be like this:
$sql = "INSERT INTO classificacoes (nossa_empresa, produtos_oferecidos,atendimento, colaboradores, indicaria) VALUES ('$star1','$star2','$star3','$star4', '$star5')";
if ($conn->query($sql) != TRUE){
echo "Erro ao gravar";
}
Or another option is to get the result of the query in a variable, like this:
$sql = "INSERT INTO classificacoes (nossa_empresa, produtos_oferecidos,atendimento, colaboradores, indicaria) VALUES ('$star1','$star2','$star3','$star4', '$star5')";
$insertOK = $conn->query($sql)
if ($insertOK != TRUE){ // This is equal to if(!$insertOK){
echo "Erro ao gravar";
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Im getting query error, im using the like LIKE sql function to search for the name submitted by the user.
But msqli_query is giving an error if i remove the LIKE function it works but doesn't works with the LIKE function
<!DOCTYPE html>
<html>
<head>
<title>Search Users</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"method="GET">
Name: <input type="text" name="name"></input>
<input type="submit" name="searchusers" value="Submit"></input> </br>
</body>
</html>
<?php require('connect.php');
$name = #$_GET['name'];
if ($_SERVER["REQUEST_METHOD"] == "GET")
{
$select123 ="SELECT username FROM users WHERE username LIKE ='%".$name."%'";
$check = mysqli_query($conn, $select123) or die("query error");
mysqli_num_rows($check) or die("Couldnt not find the Specified username");
}
?>
Please help
Remove = near keyword like :
$select123 ="SELECT username FROM users WHERE username LIKE '%".$name."%'";
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm using PHP to display what is in my MySQL database in a table. I think it is working but the output is still "ERROR". I need to delete all records in a row.
<?php
require_once ('config.inc.php');
$id=$_POST['id'];
$sql = "DELETE `subject_information` WHERE `id`='$id'";
$result = mysql_query($sql);
if ($result)
{
echo "Deleted Successfully";
}
else
{
echo "ERROR!";
mysql_close();
}
?>
You forgot your FROM keyword. The proper syntax is:
DELETE FROM table_name
WHERE some_column=some_value;
So your code should be like this:
$sql = "DELETE FROM `subject_information` WHERE `id`='$id'";
you should change this line :
$sql = "DELETE `subject_information` WHERE `id`='$id'";
to
$sql = "DELETE FROM `subject_information` WHERE `id`='".$id."'";
First you should output the error that is returned from the database:
if ($result)
{
echo "Deleted Successfully";
}
else
{
echo mysql_error();
}
Second: the mysql_xxxx functions will be removed from PHP in future version. You should have a look at PDO to connect to your database
Syntax of your query has to be changed for deleting a row from table use following syntax
$sql = "DELETE FROM tablename WHERE id='$id'";
off topic, but please read http://php.net/manual/security.database.sql-injection.php
this type of query is vulnerable for SQL-Injections, because you don't check/quote your $id.
As a hint, these functions may help you:
mysql_real_escape_string
ctype_digit
is_numeric
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I know I am doing something wrong but I really would like to know what it is. I can echo the
username of the session loggedin user using <?php echo $_SESSION['username']; ?>but I don't know why it doesn't work when I try to query database using the same technique. my codes below
I include this in the page
<?php
session_start();
$username=$_SESSION['username'];
?>
and here is the code that was suppose to display firstname and user_id of the sessions logged in user
<?php
$conn = new mysqli('localhost', 'root', 'browser', 'test');
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
$username = '$username';
$sql = "SELECT `user_id`, `firstname` FROM `members` WHERE `username`='$username'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<br /> user_id: '. $row['user_id']. ' - firstname: '. $row['firstname'];
}
}
else {
echo '0 results';
}
$conn->close();
?>
$username = '$username';
PHP variables inside single-quotes are not expanded. So now your variable is the literal string '$username', which undoubtedly won't match any user in your database.
You probably need to set $username = $_SESSION['username']; in your second PHP script.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have the following code, but for some reason I am getting an unexpected T_Variable. I can't seem to figure out where I am getting the error at. Any assistance will be greatly appreciated. Thanks
<? php
$status = mysql_query('SELECT count(*) FROM AHG WHERE `Survey Tech Initials` = 'Jeff' AND completed = 'yes');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>
See below
<? php
$status = mysql_query("SELECT count(*) FROM AHG WHERE `Survey Tech Initials` = 'Jeff' AND completed = 'yes'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>