Don't put your fingers where they don't belong.....

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
Havouza15
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Feb 21, 2017 3:44 am

Tue Mar 07, 2017 12:18 pm

Hi all!

Subject line is ofc correct. As a no programmer I should ask before trying. But I tried anyway. I put together a script I wanted to change a price in my e-shop database

Script below. It gives a 500 error. Correct it please

Code: Select all

<?php
$user = " admin_beauty"; //Användarnamn till databasen
$pass = "x8A73bP4_"; //Lösenord till databasen
$server = "localhost"; //Databasserver, brukar vara localhost men kan variera
$db = "db_beauty"; //Databasnamn

$multiplier = 0,83333333333333333333333333333333; 

$conn = mysqli_connect($server, $user, $pass, $db);

if(!$conn) {
	echo "Connection failed";
	exit;
}

$query = "SELECT id_product, wholesale_price FROM tb_product";

$result = mysqli_query($conn, $query);

while($row = mysqli_fetch_array($result)) {
	$newprice = $row['wholesale_price'] * $multiplier;
	$id = $row['id_product'];
	$updateQuery = "UPDATE tb_product SET wholesale_price='$newprice' WHERE id_product='$id'";
	$result = mysqli_query($conn, $query);
}

echo "Ready";

?>
Anders

AdoptiveSolution
New php-forum User
New php-forum User
Posts: 163
Joined: Wed Jun 15, 2016 8:35 am

Tue Mar 07, 2017 1:05 pm

Shouldn't it be :

Code: Select all

$updateresult = mysqli_query($conn, $updateQuery);
to update the database and to avoid a conflict with the $result of the SELECT query?

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Fri Mar 10, 2017 11:17 am

Do you have access to something where you can execute a SQL query (e.g. PHPMyAdmin)? If so - you could just run a query...

Code: Select all

UPDATE tb_product SET wholesale_price=wholesale_price  * 0,83333333333333333333333333333333
This would update all prices by the amount you want.

Post Reply