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 6 years ago.
Improve this question
As previously stated in my last question on WHAT a cron job is, I'm now trying to write one but seem to get a problem. This is the closest I've gotten. I want to update every record in the database by one more than what it already has.
Example if the field is 30, then when this script is run then it will become 31. But I have MULTIPLE fields and I want all of them to increment by one.
This is what I've currently wrote, but it doesn't work, but I DO get an echo of "Record Updated Successfully" but nothing changes. If I change it so that $Age = $Test and then I plug in a random number for the ID it will end up equaling Test. If I have it say $Age = $NewAge and the random ID, it changes the variable in my database to be 302. I have NO clue where it's pulling that random 302.
Any suggestions?
This is my code below:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "RR";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqlSelect = "SELECT id, Age FROM Horse";
$result = $conn->query($sqlSelect);
do{
$id = $row['id'];
$Age = $row['Age'];
$NewAge = $Age + 1;
$Test = 200;
echo $id, ' ', $Age, ' ', $NewAge; ?> <br/>
<?php
$sqlUpdate = "UPDATE Horse SET Age='$NewAge' WHERE id='$id'";
}
while($row = $result->fetch_assoc());
if ($conn->query($sqlUpdate) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
The question is very vague and hard to understand, but as #rjdown said, this will update ALL rows in the database and increment Age by 1.
Note: there is no need for fetching the records from database, nor looping over them. Just one line of SQL will update ALL rows.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "RR";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqlUpdate = "UPDATE Horse SET Age = Age + 1";
if ($conn->query($sqlUpdate) === TRUE) {
echo "All records updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i have a table register with email and username colums.
> ex - username email
> test test#gmail.com
> new new#gmail.com
SELECT * FROM Register WHERE email='test#gmail.com'
I can get this colom ,but i cant select username.i want assign username to variable
I think you want something like this if you use MySQL :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT username, email FROM MyTable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "username: " . $row["username"]. " - email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
In my opinion you're probably a beginner with web development with databases.
I recommend you w3chools. You can reed various examples in this site:
https://www.w3schools.com/php/php_mysql_select.asp
Your question is not clear, but if you are new with database I recommend you to start with PDO instead of mysqli.
<?php
//Db connection
function pdo_connect_mysql() {
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'phpcrud';
try {
return new PDO('mysql:host=' . $DATABASE_HOST . ';dbname=' . $DATABASE_NAME . ';charset=utf8', $DATABASE_USER, $DATABASE_PASS);
} catch (PDOException $exception) {
// If there is an error with the connection, stop the script and display the error.
exit('Failed to connect to database!');
}
}
// User Input
$username = 'john';
$email = 'john#gmail.com';
$sql = 'SELECT * FROM register WHERE username = ? email = ?';
$stmt = pdo_connect_mysql() ->prepare($sql);
$stmt->execute([$username, $email]);
$register = $stmt->fetchAll();
?>
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 2 years ago.
Improve this question
What I'm trying to do: I'm trying to change a specific column under the 'username' row where the username is the same as $loginuser var and change the speedrunhighscore row in that column into a new speedruhighscore.
The problem:
in the code below there is a line in which I put in bold, and that's the line I'm trying to run to change the data in my database but nothing changes in my database but the echos are all running smoothly.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "smolgames";
$speedrunhighscore = $_POST["speedrunhighscore"];
$loginuser = $_POST["loginuser"];
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("connection failed: " . $conn->connect_error);
}
$sql = "SELECT username FROM userinfos WHERE username = '" . $loginuser . "'";
$result = $conn->query($sql);
if($result->num_rows > 0){
**$sql3 = "UPDATE userinfos SET speedrunhighscore = (' . $speedrunhighscore . ') WHERE username = '" . $loginuser . "'";**
echo "updating your new highscore":
if($conn->query($sql3) === TRUE){
echo "your highscore have been updated successfully!";
}
else{
echo "Error: ". $sql3 . "<br>" . $conn->error;
}
}
else{
echo "no usernames found";
if($conn->query($sql2) === TRUE){
echo "new highscore send successfully";
}
else{
echo "Error: ". $sql2 . "<br>" . $conn->error;
}
}
$conn->close();
?>
note: the variable loginuser changes from a string I post using unity C#
For starters, you should be using prepared statements with bounded placeholders. This ensures your query is not vulnerable to SQL injection attacks, and ensures that even usernames such as O'Riley would work.
Next up, you don't need to check if the row exists before updating it -- you can just attempt to perform the update right away, and check how many rows was in fact updated.
Lastly, you should be configuring your MySQLi connection to throw exceptions on error, this means that you don't have to do individual error handling for each and every query.
<?php
// Configure MySQLi to throw exceptions on failure instead
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "smolgames";
$speedrunhighscore = $_POST["speedrunhighscore"];
$loginuser = $_POST["loginuser"];
try {
$conn = new mysqli($servername, $username, $password, $dbname);
$stmt = $conn->prepare("UPDATE userinfos
SET speedrunhighscore = ?
WHERE username = ?");
$stmt->bind_param("ss", $speedrunhighscore, $loginuser);
$stmt->execute();
$affectedRows = $stmt->affected_rows;
$stmt->close();
if ($affectedRows) {
echo "your highscore have been updated successfully!";
} else {
echo "no usernames found";
}
} catch (Exception $e) {
// Handle the exception
// Log it, send a message to the user "something went wrong"
}
You should be implementing some sort of authentication and authorization layer, as now you can just submit someone else's username with any arbitrary highscore, and you can basically update any scores in the table.
I'm trying to reduce stock number based on SKU number, the problem is when there is two or more same SKU in a table, it only reduce one time. Is there any way to solve it?
I'm using UPDATE with JOIN function to try matching norefsku in product table with skuvariasi in datapenjualan table
....
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tesdata";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "update product
join datapenjualan on datapenjualan.skuvariasi = product.norefsku
set
product.quantity=(product.quantity-datapenjualan.jumlah)
where datapenjualan.skuvariasi = product.norefsku AND datapenjualan.statuspesanan = 'PERLU DIKIRIM' "
;
if ($conn->query($sql) === TRUE) {
echo "<center><img src='animat-pencil-color.gif'><br><div class='alert alert-success' role='alert'>Update Stok Product Done. </center>";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
This code works, but the data result only valid if there is no other same SKU number on datapenjualan table. I expect the output could also works for condition where there are many same SKU in the table.
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
Here is my php for connecting to a database and querying the table "userActivityTime", in which there is one row. I am not having trouble connecting to the database (i.e. no errors), but my query is not working, and despite looking all over the internet, I cannot figure out why. Hoping you all can help. Thanks so much in advance!
<?php
// ESTABLISH TABLE AND COLUMN NAMES
$mysqli = new mysqli("*****", "****", "*****", "****");
// MAKE SURE CONNECTION SUCCEEDED
if ($mysqli_connection->connect_error) {
echo "Not connected, error: " . $mysqli_connection->connect_error;
exit();
} else {
echo "connected";
}
$query = "SELECT 'userDailyTime' FROM 'userActivityTime'";
if ($mysqli->query($query)) {
echo $mysqli->error;
}
exit();
?>
use back-ticks if necessary not single quotes:
SELECT `userDailyTime` FROM `userActivityTime`
or just
SELECT userDailyTime FROM userActivityTime
Edit:
An example from the internet, worm your stuff into the concept.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
You don't need to put the table and column names in quotes. You are telling it to echo $mysqli->error when the query is successful (prepend it with an exclamation mark) . Also, you create $mysqli then refer to it as $mysqli_connection, so I am not sure how your code works.
Try this:
<?php
// ESTABLISH TABLE AND COLUMN NAMES
$mysqli = new mysqli("*****", "****", "*****", "****");
// MAKE SURE CONNECTION SUCCEEDED
if ($mysqli->connect_errno) {
echo "Not connected, error: " . $mysqli->connect_error;
return false;
} else {
echo "connected";
}
$query = "SELECT userDailyTime FROM userActivityTime";
if (!$mysqli->query($query)) {
echo $mysqli->error;
}
return true;
?>
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'm trying to select a SINGLE value from the mysql database. I have run the query in phpmyadmin and it work great. But when I echo the $result, I get nothing... by the way,for the database and password I use xxx because I don't want to show it... My insert query works very well
Thanks
<?php
//Create Connection
$servername = "localhost";
$username = "root";
$password = "xxx";
$dbname = "xxx";
//Connect
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT StartPriceUnder FROM YJ_Value";
$result = $conn->query($sql);
echo hi;
echo $result;
echo ya;
$conn->close();
?>
Try this:
<?php
$servername = "localhost";
$username = "root";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT StartPriceUnder FROM YJ_Value";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "StartPriceUnder:" . $row["StartPriceUnder"];
}
}
else {
echo "0 results";
}
$conn->close();
?>
You have to fetch your result, so do something like this:
$row = $result->fetch_array(MYSQLI_ASSOC);
After this you can echo it like this:
echo $row["StartPriceUnder"];
For more information about fetch_array() see the manual: http://php.net/manual/en/mysqli-result.fetch-array.php