Updating a record using passed variables - php

This Update Query seems to work inside MySQL WorkBench but when applying it to my php application doesn't want to work... Both the $TotalSeats & $PerfID parameters have been tested and they output the desired number.
Is this a minor syntax error or am I missing a trick here?
$deductSeats = "UPDATE perf SET Seats=(SELECT SUM(Seats -'$TotalSeats')) WHERE PerfID = '$PerfID'";
if (mysqli_query($conn,$deductSeats))
{
echo 'Query Worked!<br>';
}
else
{
echo 'Query Didnt Work<br>';
}
$deductSeats = "deductSeats(`$TotalSeats`,`$PerfID`)";

You can try :
$deductSeats = "UPDATE perf SET `Seats` = `Seats` - ".$TotalSeats." WHERE PerfID = ".$PerfID;
if (mysqli_query($conn,$deductSeats)) {
echo 'Query Worked!<br>';
} else {
echo 'Query Didnt Work<br>';
}

Related

unable to run mysql query

For some reason I keep getting the output failed i did die() right after my $query and got the desired result but after that nothing seems to work. Can somebody point out as to what I am doing wrong?
$query="UPDATE `u313495632_test`.`users` SET `firstname='$firstname',`surname`='$surname',`gender`='$gender' WHERE `users`.`id`='$user'";
if ($query_run = mysql_query($query)) {
echo 'Profile Updated';
} else {
echo 'Failed';
}
Please try the following code:
$query="UPDATE `u313495632_test`.`users` SET `firstname`='$firstname',`surname`='$surname',`gender`='$gender' WHERE `users`.`id`='$user'";
$query_run = mysql_query($query);
if (!$query_run) {
echo 'Failed';
} else {
echo 'Profile Updated';
}
And You should use mysqli or PDO. Mysql is deprecated.
`firstname= should be `firstname`= You forgot a back tick after the field name.

Cant track error cause in PHP page updating a MS SQL database

Simple PHP page (I'm no PHP expert, just learning) to update a MS SQL database. The following code generates an error that I dont know how to solve.
include '/connections/SFU.php';
$query = "UPDATE Person SET PhotoURL = '".$file["name"]."' WHERE USERID='".$_REQUEST['user_id']."';";
if ($result = odbc_exec($dbconnect, $query)) {
echo "// Success!";
}
else {
echo "// Failure!";
}
odbc_close($dbconnect);
//End Update
This fails every time in the "if ($result ..." section
However, if I run virtually the same code
include '/connections/SFU.php';
$query = "UPDATE Person SET PhotoURL = '89990.jpg' WHERE USERID='80'";
if ($result = odbc_exec($dbconnect, $query)) {
// Success!
}
else {
// Failure!
}
odbc_close($dbconnect);
//End Update
It works just fine. I have echoed the $query string to the screen and the string is the same for both. I can't figure out why it fails in one and not the other?
Also weird is when I use a parameterized query such as
include '/connections/SFU.php';
$query = "UPDATE dbo.Person SET PhotoURL=? WHERE USERID=?";
if ($res = odbc_prepare($dbconnect,$query)) {
echo "Prepare Success";
} else {
echo "Prepare Failed".odbc_errormsg();
}
$uid = $_REQUEST['user_id'];
$fn = $file["name"];
echo "query=".$query." userid=".$uid." filename=".$fn;
if ($result = odbc_exec($res, array($fn, $uid))) {
echo "// Success!";
}
else {
echo odbc_errormsg();
echo "// Failure!";
}
odbc_close($dbconnect);
The query fails in the prepare section above, but fails in the odbc_exec section below:
include '/connections/SFU.php';
$query = "UPDATE Person SET PhotoURL=? WHERE USERID=?";
if ($res = odbc_prepare($dbconnect,$query)) {
echo "Prepare Success";
} else {
echo "Prepare Failed".odbc_errormsg();
}
$uid = "80";
$fn = "samplefile.jpg";
echo "query=".$query." userid=".$uid." filename=".$fn;
if ($result = odbc_exec($res, array($fn, $uid))) {
echo "// Success!";
}
else {
echo odbc_errormsg();
echo "// Failure!";
}
odbc_close($dbconnect);
In all cases I do not get any odbc_errormsg ().
Remove the extra ; from your query.
$query = "UPDATE Person SET PhotoURL = '".$file["name"]."' WHERE
USERID='".$_REQUEST['user_id']."';";
^
So your query should be,
$query = "UPDATE Person SET PhotoURL = '".$file["name"]."' WHERE
USERID='".$_REQUEST['user_id'];
Also have practice of using odbc_errormsg() so you can have a better idea why your query gets failed.
Warning: Your code is vulnerable to sql injection attacks!

update in mysql_query sometime return null

I use this code for update a record in mysql
This is my code:
<?php
$query = "update seeds set status='success' where id = $x";
$result = mysql_query($query);
if(!$result){
echo 'failed'.$result;
print_r($result);
}else{
echo 'successfully';
}
?>
always successfully printed out.
But when the server is crowded, the 'failed' string is printed out without any value for 'result' variable.
This query always work correctly but sometime returns NULL.
How can I fix this?
there is a issue.
i use transaction with this query.
in fact my actual code is this:
<?php
.
.
.
$QUERY_TRANSACTION = mysql_query('START TRANSACTION');
if(!$QUERY_TRANSACTION){
echo "error 101" ;
exit;
}
$seed_query = "INSERT INTO seeds VALUES('','$username','$theTime','در انتظار')";
$seed_result = mysql_query($seed_query);
if(mysql_affected_rows()!=1){
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo "error 102";
exit;
}
$seed_id = mysql_insert_id();
$_SESSION['SEED_ID'] = $seed_id;
$query_values = "(NULL,'$list_number[0]',0,$seed_id)";
for($i=1;$i<count($list_number);$i++){
$query_values .= ",(NULL,'$list_number[$i]',0,$seed_id)";
}
$r_query = "INSERT INTO rc_members VALUES $query_values";
$r_result = mysql_query($r_query);
if(mysql_affected_rows()<=0){
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo "error 103";
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
//--------------
$QUERY_TRANSACTION = mysql_query('START TRANSACTION');
if(!$QUERY_TRANSACTION){
echo "error 104" ;
exit;
}
$s_status = the_process($list_number,$m,$seed_id);
if($s_status == 'successful_proc'){
$s_query = "UPDATE seeds SET status='انجام شده' WHERE id=$seed_id";
$s_result = mysql_query($s_query);
//----------------logg file
$filename = "debug.txt" ;
$filepointer =fopen($filename ,"w") ;
fwrite($filepointer , print_r($s_result,true)) ;
fclose($filepointer) ;
//-----------------------
if(!$s_result){
echo "error 105".$s_result;
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
echo 'successfuly process';
}else{
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo 'error 106';
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
?>
that 'successfully' always printed out. but sometime 'error 105' printed out.
may be transaction is the reason of this error?
that update in db is performed but return null?
If mysql_query returned NULL, then that would be a bug on PHP. How do you know that it is actually returning NULL?
For update statements mysql_query should only return TRUE or FALSE. So your error checking code is fine. As to finding out what went wrong, you will have to call other function - mysql_error() would give you a blurb about what went wrong. So print the value of mysql_error() inside your false block. Like this:
echo 'failed. SQL Err: '. mysql_error()
Do that and you will probably get a clue to as to how 'record got updated, but return value is false'. It should not have happened.
$result in this case is just a resource, it does not contain the reason.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success or FALSE on error.
Source: PHP Manual
So you can only get TRUE / FALSE from it in your update query. It won't tell you the reason. But as you said it happens when your server is overcrowded so reason probably is "too many connections"

update in mysql_query often works correctly.but sometime return no results [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
update in mysql_query sometime return null
I use update in mysql_query in my PHP code. On localhost (in xampp) my code works correctly, but on the production host, the code often works correctly but sometime returns no result for update query.
If the update failed, it must return 'error' or '0' but it does not return any result. Why?
This is my code:
<?php
.
.
.
$QUERY_TRANSACTION = mysql_query('START TRANSACTION');
if(!$QUERY_TRANSACTION){
echo "error 101" ;
exit;
}
$seed_query = "INSERT INTO seeds VALUES('','$username','$theTime','در انتظار')";
$seed_result = mysql_query($seed_query);
if(mysql_affected_rows()!=1){
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo "error 102";
exit;
}
$seed_id = mysql_insert_id();
$_SESSION['SEED_ID'] = $seed_id;
$query_values = "(NULL,'$list_number[0]',0,$seed_id)";
for($i=1;$i<count($list_number);$i++){
$query_values .= ",(NULL,'$list_number[$i]',0,$seed_id)";
}
$r_query = "INSERT INTO rc_members VALUES $query_values";
$r_result = mysql_query($r_query);
if(mysql_affected_rows()<=0){
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo "error 103";
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
//--------------
$QUERY_TRANSACTION = mysql_query('START TRANSACTION');
if(!$QUERY_TRANSACTION){
echo "error 104" ;
exit;
}
$s_status = the_process($list_number,$m,$seed_id);
if($s_status == 'successful_proc'){
$s_query = "UPDATE seeds SET status='انجام شده' WHERE id=$seed_id";
$s_result = mysql_query($s_query);
//----------------logg file
$filename = "debug.txt" ;
$filepointer =fopen($filename ,"w") ;
fwrite($filepointer , print_r($s_result,true)) ;
fclose($filepointer) ;
//-----------------------
if(!$s_result){
echo "error 105".$s_result;
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
echo 'successfuly process';
}else{
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo 'error 106';
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
?>
always "sucsessfully process" printed.but sometime "error 105" printed without any value for $s_result;
i try to save the $s_result in file and see the no result save in file.
in other word the $s_result is null.
thank's
The problem that MySql returns false in case of no record effected or error
You can use mysql_affected_rows() after the query instead of
if(!$s_result){
echo "error 105".$s_result;
exit;
}
you can use
if(mysql_affected_rows($s_result) == -1){
echo "error 105".$s_result;
exit;
}
link for manual of the function :
mysql_affected_rows

unable to execute query

Can somebody help me with this query.The page doesn't load up whenever i run this query.I'm pretty much sure its something really simple mistake which I'm unable to figure out.Your help is much appreciated.
$aggr_nr = $_REQUEST['stck_list_nr_01'].$_REQUEST['stck_list_nr_02'].$_REQUEST['stck_list_nr_03'];
echo $aggr_nr;
$sql="SELECT v.id FROM vers_einl_aggregatnummer AS v WHERE v.aggr_nr = $aggr_nr";
$aggr_id = mysql_query($sql);
if ($aggr_id == true)
{
echo "query 1 executed".$aggr_id;
else
{
echo("<br />Could not execute statement ".$sql);
}
}
//sanitize the inputs
$aggr_nr = mysql_real_escape_string($_REQUEST['stck_list_nr_01'].$_REQUEST['stck_list_nr_02'].$_REQUEST['stck_list_nr_03']);
echo $aggr_nr;
$sql="SELECT v.id FROM vers_einl_aggregatnummer AS v WHERE v.aggr_nr = '".$aggr_nr."'";//missing Quotes
$aggr_id = mysql_query($sql);
if ($aggr_id) {
while($result = mysql_fetch_array($aggr_id))
echo "ID NUMBER:".$result['id'];
} else {
echo "<br />Could not execute statement ".$sql;
}
there was a syntax error look at this
if ($aggr_id != false)//since on success resource type is returned.
{
echo "query 1 executed".$aggr_id;
}else
{
echo("<br />Could not execute statement ".$sql);
}
also
$aggr_nr = $_REQUEST['stck_list_nr_01'].$_REQUEST['stck_list_nr_02'].$_REQUEST['stck_list_nr_03'];
echo $aggr_nr;
if(isset($aggr_nr)&&is_numeric($aggr_nr))
{
$aggr_nr=mysql_real_escape_string($aggr_nr);
$sql="SELECT v.id FROM vers_einl_aggregatnummer AS v WHERE v.aggr_nr = $aggr_nr";
$aggr_id = mysql_query($sql);
}
Just a word about PHP errors not showing (page doesn't load up).
You can configure PHP to stop on errors and not show it. This should be the default setting on a production server to not show too much internal information in case of an error.
You can use these lines to display all the errors, but don't forget to turn it off again ;)
ini_set('display_errors', 1);
error_reporting(E_ALL);

Categories