mysqli_query() doesn't insert into table [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 8 years ago.
Improve this question
Have no idea why it won't insert
$name = 'tom';
$email = 'swag';
$number = 123;
mysqli_query($connection, "INSERT INTO `table` (`name`, `email`,
`number`) VALUES ('$name', '$email', '$number')");
This is just a test to see why it won't insert, i have set the connection up and there are no errors being thrown.

According to the documentation for mysqli_query(), it returns FALSE on failure and TRUE on successful inserts.
So first, check its return value to see if it thinks it failed or succeeded. It probably thinks it failed, in which case, use mysqli_error() to get a string description of the error.

Related

sql statement in PHP doesn't update the Database [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
When I put a number myself the database gets updated to Success but when I try to access the same number from $_POST It complete the transaction but doesn't affect the rows even thought it's using the same number.
Example of a query that works perfectly and updates the database
$sql = "UPDATE `transactions` SET `status` ='Success' WHERE `transactions`.`txn_id` = 65765756";
Example of a query that doesn't work
$sql = "UPDATE `transactions` SET `status` ='Success' WHERE `transactions`.`txn_id` = ".$_POST['m_payment_id'];
First, check what's in the value, and make sure it is the same as what you are manually entering.
var_dump($_POST['m_payment_id'])
Second, the code without any other checks is a SQL injection vulnerability.
You could convert the value into an integer to protect against this, e.g. intval($_POST['m_payment_id'])
Ideally though, you would be using bindings.

SQL Syntax Error, i don't know the Correct Syntax [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 4 years ago.
Improve this question
$statement = $pdo->prepare("UPDATE config SET (name, value) VALUES(:name, :value) WHERE id = 1");
Hello, i need your Help, i know her is a syntax error but i dont know what is the syntax error.
Can you Help
Separate assignments for the set:
UPDATE config
SET name = :name,
value = :value
WHERE id = 1;
The syntax for UPDATE has not changed in MySQL and has never (to the best of my knowledge) included a VALUES clause. The documentation is pretty clear on the subject. If you don't think the documentation is clear, you can provide feedback on it.
Here is a full code:
$id = 1;
$sql = "UPDATE config SET `name`=?, `value`=? WHERE id=?";
$stmt= $dpo->prepare($sql);
$stmt->execute([$name, $value, $id]);

mysqli_stmt_affected_rows doesn't work for update prepared statement [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 5 months ago.
Improve this question
I have this code
$SQL = "UPDATE cart SET a = ? WHERE b = ? AND c = ?";
if($stmt2 = mysqli_prepare($conn, $SQL )){
mysqli_stmt_bind_param($stmt2, "iii", $a1, $b1, $c1);
mysqli_stmt_execute($stmt2);
if(mysqli_affected_rows($conn)){
echo "updated";
mysqli_stmt_close($stmt2);
}
else
echo "nope";
}
I was figuring out why above code never work. I have debug every way that I know.
Lastly I delete the if condition. Somehow, it is working. Tried to browse on the internet to figure out what is the cause but did not find any. Can somebody please explain to me why does it happen so?
You should be using
mysqli_stmt_affected_rows($stmt2);
mysqli_stmt::$affected_rows -- mysqli_stmt_affected_rows — Returns the total number of rows changed, deleted, or inserted by the last executed statement
Instead of
mysqli_affected_rows($conn);
which is for plain query executions.
Manual
Edit
I just noticed that your question title mentions the right function name, and your code uses the wrong one.

Simple sql Insertion failure? [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
I've been trying to insert a tuple into my relation, but it doesn't seem to work.
<?php
$link=mysql_connect ("connection", "name", "pw");
mysql_select_db('pizzaria');
$sql = "INSERT INTO pizza_bakery (Name, Address, Telenum)
VALUES ('Test', 'Test', '111')";
?>
The connection seems to go through ok, as I'm able to print the table out easily on my html page, why is this failing?
Your insert statement should look something like this:
$sql = "INSERT INTO pizza_bakery (Name, Address, Telenum)
VALUES ('Test', 'Test', 111)";
Reason: The last column (Telenum) is an integer datatype and the insert statement was treating it like a string.

I need to join two tables in 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 8 years ago.
Improve this question
I need to insert image row from table 'users2' to 'allbets'. But suddenlly this code don't working and I don't know why.. What is wrong with this?
$q2 = $pdo->prepare('INSERT INTO allbets (image) SELECT users2.image FROM users2 WHERE username = ?');
$q2->bindValue(1, $_SESSION['name']);
$q2 -> execute();
This code did not suddenly stop working, it never could have worked with its present query syntax. Change the query to this -
$q2 = $pdo -> prepare('INSERT INTO allbets (user, bet, komanda, teams, cof, data, image) VALUES ($user, $bet, $komanda, $teams, $cof, $data, (SELECT `users2`.`image` FROM `users2` WHERE `username` = ?));
Do yourself a service and error checking to your PHP code and to your PDO. This will let you know where to look when errors occur.

Categories