I don't know why, but for some reason the code below is not working as intended
$SQL = "UPDATE characters SET
name = '$char_name',
status = '$char_status',
gender = $char_gender,
pos.x = $char_posx,
pos.y = $char_posz,
shards = $char_money,
level = $char_level,
exp = $char_exp,
hair = $char_hair,
color.r = $char_color_r,
color.g = $char_color_g,
color.b = $char_color_b,
spawn = $char_spawn
WHERE username = '$nick'";
mysql_query($SQL) or die("ERRORCODE 04 - DB QUERY FAIL");
echo "saved";
it's always giving me the "ERRORCODE 04.." meaning that the query failed..
FYI: setting pos.y db value to the char_posz is correct as the axes are different from the Form to the actual database
EDIT: code now changed a bit due to some comments, looks now like this:
$SQL = "UPDATE characters SET
name = '$char_name',
status = '$char_status',
gender = $char_gender,
pos_x = $char_posx,
pos_y = $char_posz,
shards = $char_money,
level = $char_level,
exp = $char_exp,
hair = $char_hair,
color_r = $char_color_r,
color_g = $char_color_g,
color_b = $char_color_b,
spawn = $char_spawn
WHERE username = '$nick'";
mysqli_query($dbcon, $SQL) or die(mysqli_error($dbcon));
echo "saved";
this is the error I get:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
pos_x = ,
pos_y = ,
shards = ,
' at line 4
Try to put single quotes around all variables in the query
Related
I am trying to update multiple rows in a single query. Data doesnt get updated in my code. I am trying to join the two tables. When user enters a no. The data from the 2 tables will be displayed which is connected through the foreign key.The data from the table1 gets updated. Where as the columns from the table 2 doesnt get updated. I need to update the second table based on unique id
if($_REQUEST["profile"] == "profile")
{
$Id = $_REQUEST["id"];
$firstname = mysql_real_escape_string($_REQUEST["firstname"]);
$serial = mysql_real_escape_string($_REQUEST["serial"]);
$dom = mysql_real_escape_string($_REQUEST["dom"]);
$idno = $_REQUEST["idno"];
$pow = mysql_real_escape_string(stripslashes($_REQUEST["pow"]));
$address = mysql_real_escape_string(stripslashes($_REQUEST["address"]));
$bookno = mysql_real_escape_string(stripslashes($_REQUEST["bookno"]));
$zone = mysql_real_escape_string(stripslashes($_REQUEST["zone"]));
$mobile = mysql_real_escape_string(stripslashes($_REQUEST["phone"]));
$phone = mysql_real_escape_string(stripslashes($_REQUEST["mobile"]));
$mothertongue=mysql_real_escape_string(stripslashes($_REQUEST["mothertongue"]));
$nof=mysql_real_escape_string(stripslashes($_REQUEST["nof"]));
$email=mysql_real_escape_string(stripslashes($_REQUEST["email"]));
$nom=$_REQUEST["nom"];
$nofemale=$_REQUEST["nofemale"];
mysql_query("UPDATE profile SET firstname='".$firstname."',serial='".$serial."',dom='".$dom."',idno='".$idno."',pow='".$pow."',address='".$address."',bookno='".$bookno."',
zone='".$zone."',phone='".$mobile."',mobile='".$phone."',mothertongue='".$mothertongue."',email='".$email."',nof='".$nof."',nom='".$nom."',nofemale='".$nofemale."' WHERE id = '".$_POST['id']."' " ) or die(mysql_error());
for($i=0;$i<count($_REQUEST['slno1']);$i++)
{
$mid=$_REQUEST['mid'][$i];
$slno1 = mysql_real_escape_string(stripslashes($_REQUEST["slno1"][$i]));
$name1 = mysql_real_escape_string(stripslashes($_REQUEST["name1"][$i]));
$rhof1 = mysql_real_escape_string(stripslashes($_REQUEST["rhof1"][$i]));
$dob1 = mysql_real_escape_string(stripslashes($_REQUEST["dob1"][$i]));
$dobapt1 = mysql_real_escape_string(stripslashes($_REQUEST["dobapt1"][$i]));
$doc1 = mysql_real_escape_string(stripslashes($_REQUEST["doc1"][$i]));
$doconf1 = mysql_real_escape_string(stripslashes($_REQUEST["doconf1"][$i]));
$qualification1 = mysql_real_escape_string(stripslashes($_REQUEST["qualification1"][$i]));
$school1 = mysql_real_escape_string(stripslashes($_REQUEST["school1"][$i]));
$occupation1 = mysql_real_escape_string(stripslashes($_REQUEST["occupation1"][$i]));
$run=mysql_query("UPDATE member SET
slno1='".$slno1."',name1='".$name1."',rhof1='".$rhof1."',dob1='".$dob1."',dobapt1='".$dobapt1."',doc1='".$doc1."',doconf1='".$doconf1."',qualification1='".$qualification1."' WHERE mid = '".$mid."' " ) or die(mysql_error());
}
}
Please use PDO so you won't have to escape strings and so your code gets simpler to read. Your query has too many quotes used and this alone can make it easy to fail. Please use following examples and this should help you succeed.
Basic PDO update:
https://www.w3schools.com/php/php_mysql_update.asp
Bind Params:
https://www.w3schools.com/php/php_mysql_prepared_statements.asp
In your query you are using $_POST['mid'] instead of that you should use $mid which you are already reading as
$mid=$_REQUEST['mid'][$i];
As per my understanding UPDATE query is used to update a limited number of records if using the where condition. So the only way that I can think of is using an INSERT query with ON DUPLICATE KEY UPDATE clause. Try the below code:
for($i=0;$i<count($_REQUEST['mid']);$i++) {
$mid[] = $_REQUEST['mid'][$i];
$slno1[] = mysql_real_escape_string(stripslashes($_REQUEST["slno1"][$i]));
$name1[] = mysql_real_escape_string(stripslashes($_REQUEST["name1"][$i]));
$rhof1[] = mysql_real_escape_string(stripslashes($_REQUEST["rhof1"][$i]));
$dob1[] = mysql_real_escape_string(stripslashes($_REQUEST["dob1"][$i]));
$dobapt1[] = mysql_real_escape_string(stripslashes($_REQUEST["dobapt1"][$i]));
$doc1[] = mysql_real_escape_string(stripslashes($_REQUEST["doc1"][$i]));
$doconf1[] = mysql_real_escape_string(stripslashes($_REQUEST["doconf1"][$i]));
$qualification1[] = mysql_real_escape_string(stripslashes($_REQUEST["qualification1"][$i]));
$school1[] = mysql_real_escape_string(stripslashes($_REQUEST["school1"][$i]));
$occupation1[] = mysql_real_escape_string(stripslashes($_REQUEST["occupation1"][$i]));
}
$query = "INSERT INTO `member` (`mid`,`slno1`,`name1`,`rhof1`,`dob1`,`dobapt1`,`doc1`,`doconf1`,`qualification1`) VALUES ";
for ($i = 0; $i < count($mid); $i++) {
$query .= "('".$mid[$i]."','".$slno1[$i]."','".$name1[$i]."','".$rhof1[$i]."','".$dob1[$i]."','".$dobapt1[$i]."','".$doc1[$i]."','".$doconf1[$i]."','".$qualification1[$i]."')";
if ($i != (count($mid) - 1)) {
$query .= ',';
}
}
$query .= ' ON DUPLICATE KEY UPDATE `slno1` = VALUES(`slno1`), `name1` = VALUES(`name1`), `rhof1` = VALUES(`rhof1`), `dob1` = VALUES(`dob1`), `dobapt1` = VALUES(`dobapt1`), `doc1` = VALUES(`doc1`), `doconf1` = VALUES(`doconf1`), `qualification1` = VALUES(`qualification1`);';
$run=mysql_query($query) or die(mysql_error());
Hope This Helps.
I am writing a Php 5 and Mysql 5 page counter script. When a student having id as 'visitorid' visits a page having id 'pageid' (both int(11)) the page counter tries to log the visit in 'visitors' database. But counter is not updating in mysql db, instead the visit_counter int(4) turns to 0.Whats wrong with my code? visitdate is datetime.
<?php
$pageid = 101;
$visitorid = 234;
$sql = "SELECT * FROM visitors
WHERE pageid = ".$pageid."
AND visitorid = ".$visitorid;
$temp = mysql_query($sql) or die("Error 1.<br>".mysql_error());
$data = mysql_fetch_array($temp);
// visit_counter is a field in table
if(($data['visit_counter']) != NULL){
echo "Entery exists <br>";
// Tried below version also
$visit = " SET visit_counter = visit_counter+1";
//$visit_counter = $data['visit_counter'];
//$visit = " SET visit_counter = ".$visit_counter++ ;
// Valid SQL
// UPDATE `visitors`
// SET visit_counter = visit_counter+1
// WHERE pageid = 101 and visitorid=234
// This manual sql query updates in phpmyadmin
$sql = "UPDATE visitors ".$visit."
AND visitdate = NOW()
WHERE pageid = ".$pageid."
AND visitorid = ".$visitorid;
$temp = mysql_query($sql) or die("ERROR 3.<br>".mysql_error());
//No error is displayed on above query.
} else {
//first entry
$visit_count = "1";
$sql = "INSERT INTO visitors
(`pageid`,`visitorid`, `visitdate`, `visit_counter`)
VALUES ('".$pageid."','".$visitorid."', NOW(), '".$visit_count."')";
$temp = mysql_query($sql);
//first entry is inserted successfully
//and visit_counter shows 1 as entry.
}
?>
Can anyone tell me whats wrong with this code?
Oh! I got answer by myself. Sometimes just little errors make us go crazy..
I made a mistake in udate query.. rather than using and I should have user a comma instead. .. working well now!
i am new to php programming. I always get this error when i run my code
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE service_name = ''' at line 7
this is my code
<?php
session_start();
include('../conn/openconn.php');
if(isset($_POST['butsend'])) {
$servicename = strtoupper($_POST['txtservicename']);
$serviceurl = $_POST['txtserviceurl'];
$id_div = $_POST['select_div'];
$id_unit = $_POST['select_unit'];
$servicedesc = $_POST['txtservicedesc'];
$id = $_SESSION['service_name'];
$updateuser = "UPDATE service SET
service_name = '$servicename',
service_url = '$serviceurl',
id_div = '$id_div',
id_unit = '$id_unit',
service_desc = '$servicedesc',
WHERE service_name = '$id'";
mysql_query($updateuser) or die (mysql_error());
}
?>
i have already search all the previous question but due to my limited knowledge in the programming i
cannot find the solution. sorry for my bad english..
You have an extra comma. Remove it.
$updateuser = "UPDATE service SET
service_name = '$servicename',
service_url = '$serviceurl',
id_div = '$id_div',
id_unit = '$id_unit',
service_desc = '$servicedesc', <-- HERE
WHERE service_name = '$id'";
I'm working with MySQLi and I wanted now I made a form to edit some entries in the database and of course I want to update the data in the database. But for some reason nothing happen. No error either.. I allready used var_dump to see if the php script is getting the data from the form, works fine.
Maybe someone can find any mistakes at the update code.
$con->query("UPDATE anime SET
epcount = '".$eps."',
original_title = '".$otitle."',
japan_title = '".$jtitle."',
english_title = '".$etitle."',
genres = '".$genres."',
studio = '".$studio."',
season = '".$season."',
year = '".$year."',
state = '".$status."',
type = '".$type."',
manifestations = '".$manif."',
trailer = '".$trailer."',
description = '".$des."' WHERE id = '$animeId");
The problem in the last line
$con->query("UPDATE anime SET
epcount = '".$eps."',
original_title = '".$otitle."',
japan_title = '".$jtitle."',
english_title = '".$etitle."',
genres = '".$genres."',
studio = '".$studio."',
season = '".$season."',
year = '".$year."',
state = '".$status."',
type = '".$type."',
manifestations = '".$manif."',
trailer = '".$trailer."',
description = '".$des."' WHERE id = '".$animeId."'");
Also making the query in this style is a bad practice
Dear friend i am trying to update the recode but the following message always come up
" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 9"
the code looks fine but i do not understand what i am doing wrong can someone help.
thanks in Advance.
<?php
if(isset($_POST['edit'])){
// this id wil be pulled from the URL above.
$hot_id = $_GET['hotl'];
$hotel_name = escape_value($_POST['title']);
$hotel_star = escape_value($_POST['category']);
$shortdes = escape_value($_POST['shortdes']);
$country = escape_value($_POST['country']);
$address = escape_value($_POST['address']);
$pcode = escape_value($_POST['pcod']);
$city = escape_value($_POST['city']);
$query = "UPDATE Hotels SET
hotel_name = '{$hotel_name}',
star ='{$hotel_star}',
description = '{$shortdes}',
country = '{$country}',
hotel_address = '{$address}',
hotel_postal_code = '{$pcode}',
hotel_city = '{$city}'
WHERE hotel_id = {$hot_id}";
$result = mysql_query($query, $connection);
if(mysql_affected_rows() == 1){
//Success
}else{
die("Some thing wrong with the Upadate: ". mysql_error());
}
}else{
//error ocurred
}
?>
i am posting my question in both PHP and Mysql Forum because i do not know exaectly where is the problem.
The easiest way to debug SQL statements (as mentioned above) is to echo out your query before you submit it and see exactly what you are sending to the database. That being said, why don't you try putting some quotes(' ') around your $hot_id var:
WHERE hotel_id = '{$hot_id}'
Somewhere in the update there should be $hot_id:
"UPDATE Hotels SET
hotel_name = '{$hotel_name}', hotel_id = '{$hot_id}', etc