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";
}
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 months ago.
Improve this question
Hi i am trying to use mysqli_affected_rows function but it always return 0 can someone help. Trying to get it done theu MSQLI OOP.
<?php
$servername ="localhost";
$username ="root";
$password ="testdb";
$database ="mydb";
$conn = new mysqli($servername, $username, $password, $database);
if($conn->connect_error)
{
die ("The Database connection is not established : " .connect_error);
}
echo "connection established";
//editing record
$sql_update = "update mytbl SET fname='Nitin Sharma' where sr=2";
echo "The affected Rows :" .mysqli_affected_rows($conn);
$conn->close();
?>
Table Values:
You missed to execute your SQL query.
$conn->query($sql_update);
You need to first execute your query by mysqli_query
$sql_update = "update mytbl SET fname='Nitin Sharma' where sr=2";
$conn->mysqli_query($sql_update);
echo "The affected Rows :" .$conn->affected_rows;
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."
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
I have the next code, but it inserts two rows in the mysql database instead of one. Could ypu please take a look to the code?
Regards.
<?php
$name= $_POST['name'];
$password = $_POST['password'];
mysql_connect("localhost","username","mypass");
mysql_select_db("databaseName");
mysql_query($query ="insert into users(name,password) values ('$name','$password')");
if (mysql_query($query) === TRUE) {
echo "Record saved";
} else {
echo "Error";
}
?>
Don't call mysql_query() when you assign the $query variable. And remember to escape your data, since you're not using prepared statements.
mysql_connect("localhost","username","mypass");
$name = mysql_real_escape_string($_POST['name']);
$password = mysql_real_escape_string($_POST['password']);
$query ="insert into users(name,password) values ('$name','$password')";
if (mysql_query($query)) {
echo "Record saved";
} else {
echo "Error: " . mysql_error();
}
Dont use the mysql_query function twice,
you want to check it in if statement, then dont call it before if clause.
See this following code.
$query ="insert into users(name,password) values ('$name','$password')"
if (mysql_query($query) === TRUE) {
echo "Record saved";
} else {
echo "Error";
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
i can't seem to figure out why this query isn't running
if ( $productName && $productDescription && $productPrice ) {
// SQL
// UPDATE `prostud_tristurion`.`products` SET `product_title` = 'ajax test', `product_description` = 'Was certainty remaining engrossed applauded sir how discovery.', `product_price` = '524' WHERE `products`.`product_id` = 10;
try {
$query = "update products set product_title = :pName, product_description = :pDescription, product_price = :pPrice, where product_id = :pid";
//prepare query for excecution
$stmt = $con->prepare($query);
//bind the parameters
$stmt->bindParam(':pid', $id);
$stmt->bindParam(':pName', $productName);
$stmt->bindParam(':pDescription', $productDescription);
$stmt->bindParam(':pPrice', $productPrice);
// echo "$productPrice / $productDescription / $productName / $id\n $stmt";
var_dump($_POST);
// Execute the query
if ($stmt->execute() ) {
echo "Record was updated.";
} else {
die('Unable to update record.');
}
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
}
all i get is Unable to update record.
var_dump($_POST);
is looking good
You have an errant comma at product_price = :pPrice, where
If your code reaches the die statement then you have exceptions turned off (not recommended) but you can get the error message from the database (to log or echo) with $stmt->errorInfo()
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.