updating data using get method - php

it seems like I can't update my table
I'm using href like this:
href="data/updatefunction.php?updtch=0&teachid=<?php echo $row['id']; ?>&classid=<?php echo $classid;?>"
and the data goes here:
updatefunction.php
if(isset($_GET['updtch'])){
global $con;
$teachid = $_GET['teachid'];
$classid = $_GET['classid'];
$q = $con->query("UPDATE class SET teacher=$teachid WHERE id=$classid");
I echoed my teachid and classid they transferred without problem
mysqli error is nothing, I get this:
Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\gradingsystem\admin\data\updatefunction.php:8
Stack trace: #0 {main} thrown in C:\xampp\htdocs\gradingsystem\admin\data\updatefunction.php on line 8

In your database , data type of teacher is a string or int ?
if data type of teacher is string than you have to pass $teachid as string like
UPDATE class SET teacher = '$teachid' WHERE id = $classid

Related

PHP, Mysqli, SQL query to get value and display others

I'm trying to do something fairly complicated but I hope it makes sense in text.
So I have a link on a page which take me to post.php?postid=3
In my database there is a a field which is integer called camp_id. When for example I'm on a post which has the field camp_id with a value of 1, I want to display everything in the table that has the value of 1 in that field.
If I change the URL to post.php?postid=2 and that post has a camp_id of say 4, I would display a list of everything that has a camp_id of 4.
Anyway here is my code below and the current error at the bottom.
Here is my function:
public function getartfromcamp($campid)
{
$con = $this->db->OpenCon();
$campid = $con->real_escape_string($campid);
$stmt = "SELECT * from post WHERE camp_id = '$campid'";
$relatedlinks = $con->query($stmt);
if ($relatedlinks->num_rows > 1) {
$sql = $relatedlinks;
} else {
$sql = "No article";
echo "";
}
$this->db->CloseCon();
return $sql;
}
Here is the code on the page:
include 'postclass.php';
$postid = $_GET['postid'];
$article = new Post();
$relatedlinks = $article->getartfromcamp($postid);
?>
<div class='row'>
<?php
while ($row = $relatedlinks->fetch_assoc()) {
?>
<ul>
<ul>
<li><?php echo $row['article_name'];?></li>
</ul>
It seems to work with postid=1 but when I change it to something else I get the error below:
Fatal error: Uncaught Error: Call to a member function fetch_assoc()
on string in
C:\inetpub\wwwroot\local.test.co.uk\blog-example\camp1.php:18 Stack
trace: #0 {main} thrown in
C:\inetpub\wwwroot\local.test.co.uk\blog-example\camp1.php on line 18
Line: 18:
while ($row = $relatedlinks->fetch_assoc()) {
In function getartfromcamp, you are returning $sql string, instead of the connection object, when there is no result.
In this particular case, no result is coming, hence string is being returned. So it throws out error, as you are trying to run fetch_assoc on a string. You should let the function return connection object only, even if there are no rows being returned.
Change to following:
public function getartfromcamp($campid)
{
$con = $this->db->OpenCon();
$campid = $con->real_escape_string($campid);
$stmt = "SELECT * from post WHERE camp_id = '$campid'";
$relatedlinks = $con->query($stmt);
$this->db->CloseCon();
return $relatedlinks;
}
SideNote: You should switch to Prepared statements, to prevent SQL injection related issues.

PDO::fetchAll not working with MySQL stored procedure

I'm trying to return a row from a database by calling a stored procedure throught PHP. However, when I do this how I normally would I get a "General Error".
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error in C:\xampp\htdocs\Skilaverkefni 4\Courses\read.php:11
Stack trace:
#0 C:\xampp\htdocs\Skilaverkefni 4\Courses\read.php(11): PDOStatement->fetchAll(2)
#1 C:\xampp\htdocs\Skilaverkefni 4\index.php(13): ReadCourse('FOR3L3U')
#2 {main}
thrown in C:\xampp\htdocs\Skilaverkefni 4\Courses\read.php on line 11
Here is the code:
<?php
function ReadCourse($courseID)
{
require "dbCon.php";
$SQL = "SET #p0='" . $courseID . "'; CALL ReadCourse(#p0);";
echo "$SQL";
$logon = $pdo->prepare($SQL);
$logon->execute();
$records = $logon->fetchAll(PDO::FETCH_ASSOC);
print_r($records);
}
?>
After a long Google-session I found out that the issue is most caused by the way I'm handling the reading of the data returned from the stored procedure, how do I do this correctly?
The whole point of PDO is to make it impossible to run any old database query you choose to pass as a string.
From the documentation:
<?php
/* Call a stored procedure with an INOUT parameter */
$colour = 'red';
$sth = $dbh->prepare('CALL puree_fruit(?)');
$sth->bindParam(1, $colour, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 12);
$sth->execute();
print("After pureeing fruit, the colour is: $colour");
?>

Can't select from sys.extended_properties using PHP PDO

When querying a SQL Server database from PHP with this statement:
SELECT [Value] FROM sys.extended_properties WHERE name='MS_Description'
I get this error message:
"Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IMSSP]: Invalid type.' in C:\inetpub\wwwroot\index.php:104 Stack trace: #0 C:\inetpub\wwwroot\index.php(104): PDOStatement->fetch(2) #1 {main} thrown in C:\inetpub\wwwroot\index.php on line 104"
The php that I'm using is below, line 104 is the start of the While loop:
$sql = "SELECT [Value] FROM sys.extended_properties WHERE name='MS_Description'";
global $pdo;
$qry = $pdo->query($sql);
if (!$qry) {
exit('<pre>' . print_r($qry->errorInfo()));
}
while($row = $qry->fetch(PDO::FETCH_ASSOC)) {
foreach ($row as $key => $value) {
echo '<p>Key: ' .$key. ', Value: ' .$value. '</p>';
}
}
The code above works fine when selecting from any other table or view e.g:
SELECT * FROM sys.events
If I execute the statement via SSMS it works fine, any ideas why it doesn't work via PHP?
Casting the columns being selected as varchar(255) solved the problem i.e.
$sql = "SELECT CAST([Value] AS VARCHAR(255)) FROM sys.extended_properties WHERE name='MS_Description'";
Got to the solution based on the answers to this question and this question.

Echo or print an declared variable in a pdo query

So i have this query and i want it to echo it or print it somehow.
$my_query = $db->prepare("DECLARE #item varbinary(1728); SET #item = (SELECT Inventory FROM Character WHERE Name='CharName'); print #item");
$my_query->execute();
$my_query = $my_query->fetch();
echo $my_query[0] // give me error
But is not working is giving me error.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[24000]: Invalid cursor state: 0
Problem:
You cannot call multiples queries in prepare() method, only one.
Solution:
Move the SQL into a stored procedure, then call the stored procedure to return the value

Unable to compare valuesfrom mysql in a prepared statement

I can't seem to get this to connect to the database so that I can run my prepared statement. Does anybody have an idea what I've forgotten?
private function check_credentials($plain_username, $password)
{
global $dbcon;
$ac = new ac();
$ac->dbconnect();
$userid = $dbcon->prepare('SELECT id FROM users WHERE username = :username AND password = :password LIMIT 1');
$userid->bindParam(':username', $plain_username);
$userid->bindParam(':password', $password);
$userid->execute();
$id = $userid->fetch();
Return $id;
}
EDIT: I changed the SQL query from a SELECT FROM query, to an INSERT INTO query and it worked. WHat the heck is going on?
Reformatting your stack backtrace:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound' in E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php:61
Stack trace:
#0 E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php(61): PDOStatement->execute(Array)
#1 E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php(34): ac->check_credentials('joe', '94a02c32b6ff629...')
#2 E:\PortableApps\xampp\htdocs\SN\UI\UIclass.php(17): ac->authentication()
#3 E:\PortableApps\xampp\htdocs\SN\index.php(4): ui->start()
#4 {main} thrown in E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php on line 61
Is there any reason you're instantiating a new ac object in the check_credentials function? Given that check_credentials is already a method of ac, this seems odd. Does dbconnect overwrite the global dbcon?

Categories