Insert at specific 'ID' into my table. - php

I need to write a PHP script using a database for school. Since my MySQL skills are currently dying, I would like to ask following question:
Following problem: I need to insert 'upload' at a specific 'ID' into my table. It should make something like this:
insert into PHP_Project() where ID = ;
$pdo = new PDO($this->dsn, $this->username, $this->password, $this->pdoAttributes);
$statement = $pdo->prepare('insert into PHP_Project(upload) where ID = ? values (?,?)');
$statement->bindParam(1, $filename);
$statement->bindParam(2, $projectID);
$statement->execute();
I hope somebody can help me. Thanks in advance!!

The MYSQL query you wrote is incorrect. You need the MYSQL UPDATE query for updating the value at a specific column.
$statement = $pdo->prepare('UPDATE PHP_Project SET upload = ? WHERE ID = ?');
Check out this tutorial Mysql update tutorial

Related

MySQL insert not working on new table

I am doing an insert on the MySQL table "analytics" with the fields "a_id" (PRIMARY and UNIQUE), "a_query" and "a_date".
My code:
function queryanalytics($clsendquery) {
$datetime = date("Y-m-d H:i:s");
$connection = connectsql();
$sql = "INSERT INTO analytics (a_query,a_date) VALUES (?,?)";
$stmt = $connection->prepare($sql);
$stmt->bind_param('ss',$clsendquery,$datetime);
$stmt->execute;
$stmt->close();
}
Nothing updates in the database and $stmt->affected_rows returns 0.
There are no errors in $stmt->error or $connection->error.
When I run the insert in phpMyAdmin it works fine.
I have other selects and inserts that work fine with the same connection, why does this one not?
Please help me.
$stmt->execute; should be $stmt->execute();

Getting the id of the last inserted record from an MSSQL table using PDO and PHP

I am trying to get the id of the last record inserted in an mssql database using pdo via php. I HAVE read many posts, but still can't get this simple example to work, so I am turning to you. Many of the previous answers only give the SQL code, but don't explain how to incorporate that into the PHP. I honestly don't think this is a duplicate. The basic insert code is:
$CustID = "a123";
$Name="James"
$stmt = "
INSERT INTO OrderHeader (
CustID,
Name
) VALUES (
:CustID,
:Name
)";
$stmt = $db->prepare( stmt );
$stmt->bindParam(':CustID', $CustID);
$stmt->bindParam(':Name', $Name);
$stmt->execute();
I have to use PDO querying an MSSQL database. Unfortunately, the driver does not support the lastinsertid() function with this database. I've read some solutions, but need more help in getting them to work.
One post here suggests using SELECT SCOPE_IDENTITY(), but does not give an example of how incorporate this into the basic insert code above. Another user suggested:
$temp = $stmt->fetch(PDO::FETCH_ASSOC);
But, that didn't yield any result.
If your id column is named id you can use OUTPUT for returning the last inserted id value and do something like this:
$CustID = "a123";
$Name="James"
$stmt = "INSERT INTO OrderHeader (CustID, Name)
OUTPUT INSERTED.id
VALUES (:CustID, :Name)";
$stmt = $db->prepare( stmt );
$stmt->bindParam(':CustID', $CustID);
$stmt->bindParam(':Name', $Name);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
echo $result["id"]; //This is the last inserted id returned by the insert query
Read more at:
https://msdn.microsoft.com/en-us/library/ms177564.aspx
http://php.net/manual/es/pdo.lastinsertid.php

Player score update with PHP MySQL

I have a users table where I want to update the scores each time a user finishes the game. Unityscript part is working fine but after I post the score to the database it appears doubled or tripled. I post the score as int and also the table column is of int format. My PHP looks like this:
try {
$db = new PDO("mysql:host=$host;dbname=$dbname", $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = array(
':username' => $_POST['username'],
':score' => $_POST['score']);
$statement = $db -> prepare ("UPDATE users SET score = score + :score
WHERE username = :username");
$statement->execute($data);
}
catch(PDOException $e) {
echo $e->getMessage();
}
Any help or advice is appreciated.
You are using prepared statements, but you are still allowing injection by directly implementing the $score variable. Do the same thing with score that you did with username.
What do you mean by double or triple? Do you mean that the number is two or three times bigger? If so, try using a SELECT statement to fetch the score and do the math in PHP. Then, UPDATE the users table.
Doing this will allow you to better understand what you are doing wrong. Have you tried echoing the value of score within your try and catch to see if the value repeats? The code may be running more than once.
$statement = $db -> prepare ("UPDATE users SET score = :score
WHERE username = :username");
use this, i think it will work

MySQL Update gotcha? Is there something specific about UPDATE?

I have a really simple procedure I need to do, and no matter how much I debug or simplify, the record is not updating in the dbase. Assume everything is correct in terms of connection, etc. Pulling this from php and doing a MySQL call in PHPMyAdmin results in a correct record update on the table. I've tried using/not using quotes around adminId.
Any ideas?
$sampleString = "343r34c3cc43";
//Need to store the customer ID from sub system
$stmt2 = $mysqli->prepare("
UPDATE
admins
SET
chargebeeId = '?'
WHERE
adminId='22'
");
$stmt2->bind_param('s',
$mysqli->real_escape_string($sampleString)
);
$stmt2->execute();
For reference, adminId will be dynamic, with a bind_param 'i' in the application.
change this
chargebeeId = '?'
to
chargebeeId = ?
try this
$sampleString = "343r34c3cc43";
$sampleString = $mysqli->real_escape_string($sampleString) ;
$stmt2 = $mysqli->prepare("UPDATE admins
SET chargebeeId = ?
WHERE adminId='22' ");
$stmt2->bind_param('s', $sampleString);
$stmt2->execute();

Update data in database with pdo?

I recently made a CMS with mySQL and PDO following a video tutorial from PHPAcademy on youtube.
I want to add an edit function to it though.
I have the page done, but not the PHP to update the data.
So, I'm wondering, how would I update data in the database with PDO and save the new data?
I have this:
$query = $pdo->prepare("UPDATE articles 'article_content'=? WHERE 'id' = ?");
$query->bindValue(1, $_POST['content']);
$query->bindValue(2, $_POST['id']);
$query->execute()
;
You have 2 issues:
"UPDATE articles SET `article_content`=? WHERE `id` = ?"
One you are missing SET.
Two you are using single quotes for column names;use backticks
You are missing the keyword SET in your query
It should be
$pdo->prepare("UPDATE articles SET `article_content`=? WHERE `id` = ?");
----^

Categories