UPDATE prepared statement PHP Error - php

I am creating a dynamic page which changes depending on which ever post the user clicks onto. I am also wanting the views (hit-counter) the page gets to go up by one each time the page is loaded. I am currently getting the following error.
Fatal error: Call to a member function bind_param() on a non-object in C:\Users\PC\Documents\XAMPP\htdocs\post.php on line 13
<?php
session_start();
include 'php/config.php';
$post = $_GET['post'];
$stmt = $mysqli->prepare("SELECT * FROM forum WHERE ForumId = '$post'");
$stmt->execute();
$stmt->bind_result($ForumId,$ForumTitle,$ForumPostText,$PostDate,$Views);
$stmt->fetch();
$stmt->close();
$Views = 1;
$stmt = $mysqli->prepare("UPDATE 'forum' SET 'Views' = 'Views'+ 1 WHERE 'ForumId' = '?' ");
$stmt->bind_param('i',$post);
$stmt->execute();
$stmt->close();
?>
<!DOCTYPE html>
// The rest of the webpage yada yada yada

Remove (') single quotes in update query and use backtick (`) instead
So
"UPDATE `forum` SET `Views` = Views+ 1 WHERE `ForumId` = ?"

Although Krish R's response is the solution, one of the things you will want to do in cases like this, is look at $mysqli->error to actually get an error message. This will tell you that you have a syntax error near 'forum' SET 'Vi.... That in itself should indicate that that specific character (the first ' in the string) is the most likely cause of the error.

It seems you have a problem in the query.
Take note, that PDO statement dont need single quotes
Try with this:
$stmt = $mysqli->prepare("UPDATE forum SET Views = Views+ 1 WHERE ForumId = ?");
$stmt->bind_param('i', $post);

<?php
session_start();
include 'php/config.php';
$post = $_GET['post'];
$stmt = $mysqli->prepare("SELECT * FROM forum WHERE ForumId = $post");
$stmt->execute();
$stmt->bind_result($ForumId,$ForumTitle,$ForumPostText,$PostDate,$Views);
$stmt->fetch();
$stmt->close();
$Views = 1;
$stmt = $mysqli->prepare("UPDATE forum SET Views = Views + 1 WHERE ForumId = ?");
$stmt->bind_param('i', $post);
$stmt->execute();
$stmt->close();
?>
<!DOCTYPE html>

Related

I'm having problem with my sample project

edited...
hi guys can anyone help me with my website
I just want to get a specific comment in the comment area just like in facebook
<?php
require 'db.php';
$sql = 'SELECT * FROM comment WHERE postID = postID';
$statement = $connection->prepare($sql);
$statement->execute();
$comment = $statement->fetch(PDO::FETCH_OBJ);
?>
what I mean is that if I comment in the first post it will just fetch the current comment and it will not display on the other post.
Here's what your query is currently doing:
get records from the table comment where the field postID = postId
Which of course, won't work, you're using a string (without quotes) as your value. You need to bind the parameter and pass the value in, e.g.
$sql = 'SELECT * FROM `my_table` WHERE `some_field` = :myVal;';
$res = $conn->prepare($sql);
$res->execute([':myVal' => $_GET['id']]);
$data = $res->fetchAll(PDO::FETCH_ASSOC);
Here, I pass the bind a param (:myVal) and then prepare that statement. You then pass in the value for :myVal (in this case, a $_GET of the query param id).
Then fetch, var_dump/print_r that, and you should see table data in your script.
I believe answer would look like this:
<?php
require 'db.php';
$postId = 1; // get it somewhere, probably from $_GET
$sql = 'SELECT * FROM comment WHERE postID = ?';
$statement = $connection->prepare($sql);
$statement->execute( [$postId]);
$comment = $statement->fetch(PDO::FETCH_OBJ);
?>
Explanations, copied from comment to original question:
Your SQL condition postID = postID literally says any comment. You need to pass actual post ID there.

MySQL SELECT query returning false when prepared

My file should get all users with this id (It's only one since id is unique in this table) and prepare a statement to execute later. When I execute it I get this error:
Fatal error: Uncaught Error: Call to a member function execute() on
boolean in C:\xampp\htdocs\Gamanware.ga\Admin\update.php:7 Stack
trace: #0 {main} thrown in
C:\xampp\htdocs\Gamanware.ga\Admin\update.php on line 7.
And I can't see anything wrong with it. The id is alright (I echo it out to be sure), Im not using reserved words and have made sure that it won't matter anyway, but I still get this error. I have been on several forums and many questions have not worked for me. I hope some of you can! My code:
<?php
require '../includes/login_system.dbh.php';
$id = $_GET['id'];
$sql = 'SELECT * FROM `users` WHERE `id`=:id';
$statement = $conn->prepare($sql);
$statement->execute([':id' => $id ]);
Try the code below and see if it helps
require '../includes/login_system.dbh.php';
$sql= "SELECT * FROM users WHERE id = :id";
$statement = $conn->prepare($sql);
$statement->bindParam(':id', $id, PDO::PARAM_INT);
$id = $_GET['id'];
$statement->execute();
You can also do an if else statement with your execute like so to see what it gives you.
require '../includes/login_system.dbh.php';
$sql= "SELECT * FROM users WHERE id = :id";
$statement = $conn->prepare($sql);
$statement->bindParam(':id', $id, PDO::PARAM_INT);
$id = $_GET['id'];
if ($statement->execute()) {
echo "Success";
} else {
echo "Failed";
}

simple update and insert mysqli in php code can not be executed [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 5 years ago.
This little piece of code should be very easy basic coding, yet it doesn't work. The problem is within the INSERT / UPDATE code, because if I delete those and just echo simple text inside of the if/else code everything works just fine.
This is the code I have, whichs gives a HTTP ERROR 500.
$sql2 = mysqli_query($mysqli, "SELECT * FROM koppel WHERE userid = ".$_GET['userid']." AND msgid = ".$_GET['msgid']."");
$row = mysqli_fetch_assoc($sql2);
$check = $_GET['check'];
$msgid = $_GET['msgid'];
$userid = $_GET['userid'];
$ja = 'ja';
$nee = 'nee';
$tabel_content = $row['check'];
$tabel_id = $row['id'];
if ($tabel_content == $ja){
$stmt = $mysqli->prepare("UPDATE koppel SET check = ? WHERE id = ?");
$stmt->bind_param('si',
$nee,
$tabel_id);
$stmt->execute();
$stmt->close();
} elseif ($tabel_content == $nee){
$stmt = $mysqli->prepare("UPDATE koppel SET check = ? WHERE id = ?");
$stmt->bind_param('si',
$ja,
$tabel_id);
$stmt->execute();
$stmt->close();
} else {
$stmt = $mysqli->prepare("INSERT INTO koppel(userid,
msgid,check) VALUES (?, ?, ?)");
$stmt->bind_param('iis', $userid,
$msgid,
$check);
$stmt->execute();
$stmt->close();
}
What am I missing?
I don't see any error there, but make sure $mysqli is a valid mysqli connection to your database.
To debug your problem, try checking your server error logs (they will show the cause of your 500 error, and in which line) or try removing each part of your code until you understand exactly which line is failing.
You can also move all your "execute" and "close" calls to be below the if/elseif/else structure, as it always gets executed, to avoid repeating code.
Also "tabel" should be spelled "table".

Passing variable from URL + Selecting from SQL and Echoing it

Title is a little hard to understand, so basically I'm making a Pastebin clone and am attempting to do a kind of viewmember.php?id=1213 thing for viewing pastes. However, I can't figure it out at all. I've done a lot of research, and after finally understanding what I had to do (or so I thought) I made this up and don't know why it isn't working.
<?php
require 'connection.php';
$getid = $_GET["id"];
$sql = "SELECT * FROM pasteinfo WHERE id = ?";
$stmt = $con->prepare($sql);
$stmt->bind_param("i", $getid);
echo $stmt;
?>
I'm probably just stupid. Thanks for the help.
You need to run the command to execute the query.
$sql = "SELECT field1, field2 FROM pasteinfo WHERE id = ?"; // Specify fields in query
$stmt->bind_param("i", $getid); /* bind parameters for markers */
$stmt->execute(); /* execute query */
$stmt->bind_result($field1, $field2); /* bind result variables */
$stmt->fetch(); /* fetch value */
echo "Field 1:".$field1;
echo "Field 2:".$field2;
Reference: Example #1 mysqli::prepare() example
// save result in a variable and then run a loop and echo
$result = $stmt->execute();
foreach($result as $val){
echo $val->item_name;
}

Can't seem to get $_GET['id'] to work

I'm trying to get data from a database but cant seam to get the following working, does anyone know why?
I have tried print_r too and shows nothing, the db connection and everything is working too.
$id = $_GET['id'];
$query = $db->prepare('SELECT id, title, content FROM articles WHERE id = $id');
$query->execute();
$article = $query->fetchall();
This does however print_r the id from the URL correctly.
print_r($_GET);
Any help is appreciated!
Important note:
Variables inside single qoutes are not interpolated. They're just literal strings
Much better to bind them properly:
$id = $_GET['id'];
$query = $db->prepare('SELECT id, title, content FROM articles WHERE id = :id');
$query->bindParam(':id', $id);
$query->execute();
$article = $query->fetchAll(PDO::FETCH_ASSOC);

Categories