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.
Related
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>';
}
How to return a success/failure message after running the query and how to check for exist data in product_code data array before saving the data?
$query = mysql_query("INSERT INTO `produk2`
(product_code,product_name,product_desc,product_type,product_price,product_img,product_img_name)
VALUES ('$kod','$namaproduk','$spesifikasi','$jenis','$harga','$image','$name')");
}
I tried to run this code and doesn't seems to work. Any ideas? I've gone wrong?
$result=mysql_query($query);
if($result)
{
echo "<br/>Data uploaded.";
}
else
{
echo "<br/>Data not uploaded.";
}
You should use mysql_error() or mysql_errno().
See the manual
manual
Add if else condition to the return result of query.
$query = mysql_query("INSERT INTO `produk2`
(product_code,product_name,product_desc,product_type,product_price,product_img,product_img_name)
VALUES ('$kod','$namaproduk','$spesifikasi','$jenis','$harga','$image','$name')");
if($query)
{
echo "Data inserted";
}
else
{
echo "Sorry! Data not inserted";
}
What is the problem in my code ? every time it echo This image used as cover image , but delete query work properly.how can i fix it?
<?php
session_start();
if(!empty($_SESSION['userId']) && !empty($_SESSION['name'])){
include ('connect.php');
dbConnect();
if (isset($_GET['ximgid'])) {
$del=mysql_query("Delete FROM project_image WHERE ximgid='".$_GET['ximgid']."' And coverflag !='1'");
$delete=mysql_num_rows($del);
if($delete==1){
echo "<script type='text/javascript'>alert('Sucessfully Delete !!!')</script>";
echo "<script>javascript:window.location = 'projectImage.php'</script>";
}else{
echo "<script type='text/javascript'>alert('This Image Used As Cover Image')</script>";
echo "<script >window.location.href = 'projectImage.php'</script>";
}
}else{
echo "<script>javascript:window.location = 'index.php'</script>";
}
}
?>
mysql_num_rowsonly works with selectstatement.
For delete statements you have to use mysql_affected_rows
Here the part of the documentation of mysql_affected_rows:
Retrieves the number of rows from a result set. This command is only
valid for statements like SELECT or SHOW that return an actual result
set. To retrieve the number of rows affected by a INSERT, UPDATE,
REPLACE or DELETE query, use mysql_affected_rows().
mysql_affected_rows() is the way to go
$delete=mysql_affected_rows($del);
if($delete>0){
echo "<script type='text/javascript'>alert('Sucessfully Delete !!!') </script>";
echo "<script>javascript:window.location = 'projectImage.php'</script>";
}else if($delete==0){
echo "<script type='text/javascript'>alert('This Image Used As Cover Image')</script>";
echo "<script >window.location.href = 'projectImage.php'</script>";
}else{
echo "<script type='text/javascript'>alert('mysql error')</script>";
}
I had to solve my problem in an alternative way which is given below:
<?php
session_start();
if(!empty($_SESSION['userId']) && !empty($_SESSION['name'])){
include ('connect.php');
dbConnect();
if(isset($_GET['ximgid'])){
$selQuery = "SELECT coverflag FROM project_image WHERE ximgid ='".trim($_GET['ximgid'])."' AND zid = '1000' AND xpid = '".trim($_GET['pid'])."'";
$flagtResult=mysql_query($selQuery);
$flagRow=mysql_fetch_array($flagtResult);
mysql_free_result($flagRow);
$cover=$flagRow[0];
if($cover == 1){
echo "<script type='text/javascript'>alert('Used as cover picture')</script>";
echo "<script>javascript:window.location = 'projectImage.php'</script>";
}else{
$sql = "DELETE FROM project_image WHERE ximgid = '".trim($_GET['ximgid'])."' AND coverflag != '1' AND zid = '1000' AND xpid = '".trim($_GET['pid'])."'";
$result = mysql_query($sql) or die(mysql_error());
if($result > 0){
echo "<script type='text/javascript'>alert('Sucessfully Detele !!!')</script>";
echo "<script>javascript:window.location = 'projectImage.php'</script>";
}
}
}
}else{
echo "<script>javascript:window.location = 'index.php'</script>";
}
?>
Now I can get my proper output.. Thanks Everyone.
first of all it appears that you are escaping ximgid and coverflag with ' which would throw a syntax error in your sql if those were not varchar fields.
$del=mysql_query("Delete FROM project_image WHERE ximgid=".$_GET['ximgid']." And coverflag !=1;");
Second issue to address would be that you are taking a $_GET parameter and putting it directly into your SQL query which is just trouble looking to happen so I would suggest next changing it to:
$del=mysql_query("Delete FROM project_image WHERE ximgid=".mysql_real_escape_string($_GET['ximgid'])." And coverflag !=1;");
if you need extra help debugging your SQL you can always temporarily put a call to
echo mysql_error();
after your call to mysql_query in order to show the error message from the server.
I have the next php code:
<?php
mysqli_report(MYSQLI_REPORT_ALL);
$mysqli = new mysqli("localhost","mybd","mypass");
if ($mysqli->connect_errno) { echo "Error connect<br/>"; }
else {
$mysqli->select_db("database1");
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]); // shows correct database selected
$result->close();
}
$sentencia = $mysqli->prepare("select pass from users Where name ='ronald'");
echo "Prepare error:".$mysqli->error."<br/>";
if (!$sentencia) echo "<br/>sentencia is null<br/>";
if ($sentencia->execute)
{
$sentencia->bind_result($cpass);
$sentencia->fetch();
echo "Passwd:".$cpass."<br/>";
$con="checkpass";
if (($con!=$cpass) && (md5($con)!=$cpass))
{
echo "OK<br/>";
}
else echo "NO OK<br/>";
}
else echo "<br/>Error execute: ".$mysqli->error;
}
mysqli_report(MYSQLI_REPORT_OFF);
?>
Problems are:
- $mysqli->error shows nothing. No error. Always empty string.
- $sentencia->execute always return null, and then always echo "Error execute:", but no information about error.
Database selected shows ok. It select the right database. This is an example. Really the name is passed with "$sentencia->bind_param("s",$user);" but with this, I get apache error of "no object".
I don't know why it happens. The SQL is checked and is Ok.
Thanks.
Shouldn't execute be a function nor property?
http://php.net/manual/en/mysqli-stmt.execute.php
if ($sentencia->execute())
{
}
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"