/    Sign up×
Community /Pin to ProfileBookmark

why could not be able to update…

hi, I tried to change update query from mysql to mysqli, at first under mysql is works great to update into mysql database but when i changed to mysqli is not doing update it. can anyone tell me what did i missed!

under mysql

[code=php]<?php
include(‘db.php’);
if(isset($_GET[‘status’]))
{
$status1=$_GET[‘status’];
$select=mysql_query(“select * from manage where id=’$status1′”);
while($row=mysql_fetch_object($select))
{
$status_var=$row->status;
if($status_var==’0′)
{
$status_state=1;
}
else
{
$status_state=0;
}
$update=mysql_query(“update manage set status=’$status_state’ where id=’$status1′ “);
if($update)
{
header(“Location:index.php”);
}
else
{
echo mysql_error();
}
}
?>
<?php
}
?>[/code]

and under mysqli

[code=php]<?php
include(‘db.php’);
$dbcs = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
if(isset($_GET[‘status’]))
{
$status1=$_GET[‘status’];

$sql = “select * from product where product_id=’$status1′”;
$result=mysqli_query($dbcs,$sql);

while($row=mysqli_fetch_object($result))
{
$status_var=$row->status;
if($status_var==’0′)
{
$status_state=1;
}
else
{
$status_state=0;
}

$mysqli->query(“UPDATE product set status=’$status_state’ where product_id=’$status1′ “);
if($mysqli)
{
header(“Location:item.php”);
}
else
{
echo mysqli_error();
}
}
?>
<?php
}
?>[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 04.2014 — First problem I see is that you instantiate your MySQLi object as [B][FONT=Courier New]$dbcs[/FONT][/B], but then later try to use the undefined [B][FONT=Courier New]$mysqli[/FONT][/B] variable.

In general, it's a good idea to check the return values from query calls and such to see if they're false, so that you can log/output some debug info at that point.

Also, using $_GET (or other external) values directly in your SQL is asking for trouble. Since you're moving up to the world of MySQLi, why not use prepared statements with bound parameters, so that you don't have to worry about SQL injection errors/attacks?
×

Success!

Help @Alidad spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...