Index causes and Error in Updating Row - php

Why does Index causes an error in MySQL Update row? Removing it makes the Query work.
The error is
MySQLi failed to query: UPDATE quests SET id = '1', FactionID = '2', ReqReputation = '2', ReqClassID = '3', ReqClassPoints = '2', Name = '2', Description = '2', EndText = '2', Experience = '2', GExperience = '2', Copper = '2', Silver = '2', Gold = '2', Coins = '2', Reputation = '2', ClassPoints = '2', RewardType = 'S', Level = '2', Upgrade = '1', Once = '1', Slot = '2', Value = '2', Field = '2', Index = '2', Badges = '3', Guild = '1', GiveMembership = '0', War = '1' WHERE id = 1 - December 20, 2016, 1:58 am
$id = $_POST["Id"];
$FactionID = $_POST["Faction"];
$ReqReputation = $_POST["RequiredReputation"];
$ReqClassID = $_POST["Class"];
$ReqClassPoints = $_POST["ReqClassRank"];
$Name = $_POST["Name"];
$Description = $_POST["Description"];
$EndText = $_POST["EndText"];
$Experience = $_POST["Experience"];
$GExperience = $_POST["GuildExperience"];
$Copper = $_POST["Copper"];
$Silver = $_POST["Silver"];
$Gold = $_POST["Gold"];
$Coins = $_POST["Diamond"];
$Reputation = $_POST["Reputation"];
$ClassPoints = $_POST["ClassPoints"];
$RewardType = $_POST["RewardType"];
$Level = $_POST["Level"];
$Upgrade = $_POST["Upgrade"];
$Once = $_POST["Once"];
$Slot = $_POST["Slot"];
$Value = $_POST["Value"];
$Field = $_POST["Field"];
$QIndex = $_POST["Index"];
$Badges = $_POST["Badges"];
$Guild = $_POST["Guild"];
$GiveMembership = $_POST["GiveMembership"];
$War = $_POST["War"];
$MYSQL_QUERY = $content->DBase('Query', array( 0 => "UPDATE quests SET id = '$id', FactionID = '$FactionID', ReqReputation = '$ReqReputation', ReqClassID = '$ReqClassID', ReqClassPoints = '$ReqClassPoints', Name = '$Name', Description = '$Description', EndText = '$EndText', Experience = '$Experience', GExperience = '$GExperience', Copper = '$Copper', Silver = '$Silver', Gold = '$Gold', Coins = '$Coins', Reputation = '$Reputation', ClassPoints = '$ClassPoints', RewardType = '$RewardType', Level = '$Level', Upgrade = '$Upgrade', Once = '$Once', Slot = '$Slot', Value = '$Value', Field = '$Field', Index = '$Index', Badges = '$Badges', Guild = '$Guild', GiveMembership = '$GiveMembership', War = '$War' WHERE id = $id"));
Function DBase
/** MYSQL IMPROVED EXTENSION (PARENT CLASS) **/
public function DBase($type, $params = array()) {
if (!$this->SITE->CMS->Connection)
SystemExit('No available MySQLi connection', __LINE__, __FILE__);
switch (strtoupper($type)) {
case 'QUERY':
if ($Query = parent::query($params[0])) {
$this->SITE->CMS->TotalQuery++;
return $Query;
} else
SystemExit('MySQLi failed to query: ' . $params[0], __LINE__, __FILE__);
break;
case 'PREPARE':
if ($Query = parent::prepare($params[0])) {
$this->SITE->CMS->TotalQuery++;
return $Query;
} else
SystemExit('MySQLi failed to prepare: ' . $params[0], __LINE__, __FILE__);
break;
case 'ESCAPESTRING':
if ($Escape = parent::real_escape_string($params[0]))
return $Escape;
else
SystemExit('MySQLi failed to escape: ' . $params[0], __LINE__, __FILE__);
break;
}
}
Here is the Table Quest

You're using the wrong variable for index. You define it above as $QIndex but then refer to it as $Index on the last line. Try the following:
$id = $_POST["Id"];
$FactionID = $_POST["Faction"];
$ReqReputation = $_POST["RequiredReputation"];
$ReqClassID = $_POST["Class"];
$ReqClassPoints = $_POST["ReqClassRank"];
$Name = $_POST["Name"];
$Description = $_POST["Description"];
$EndText = $_POST["EndText"];
$Experience = $_POST["Experience"];
$GExperience = $_POST["GuildExperience"];
$Copper = $_POST["Copper"];
$Silver = $_POST["Silver"];
$Gold = $_POST["Gold"];
$Coins = $_POST["Diamond"];
$Reputation = $_POST["Reputation"];
$ClassPoints = $_POST["ClassPoints"];
$RewardType = $_POST["RewardType"];
$Level = $_POST["Level"];
$Upgrade = $_POST["Upgrade"];
$Once = $_POST["Once"];
$Slot = $_POST["Slot"];
$Value = $_POST["Value"];
$Field = $_POST["Field"];
$QIndex = $_POST["Index"];
$Badges = $_POST["Badges"];
$Guild = $_POST["Guild"];
$GiveMembership = $_POST["GiveMembership"];
$War = $_POST["War"];
$MYSQL_QUERY = $content->DBase('Query', array( 0 => "UPDATE quests SET id = '$id', FactionID = '$FactionID', ReqReputation = '$ReqReputation', ReqClassID = '$ReqClassID', ReqClassPoints = '$ReqClassPoints', Name = '$Name', Description = '$Description', EndText = '$EndText', Experience = '$Experience', GExperience = '$GExperience', Copper = '$Copper', Silver = '$Silver', Gold = '$Gold', Coins = '$Coins', Reputation = '$Reputation', ClassPoints = '$ClassPoints', RewardType = '$RewardType', Level = '$Level', Upgrade = '$Upgrade', Once = '$Once', Slot = '$Slot', Value = '$Value', Field = '$Field', Index = '$QIndex', Badges = '$Badges', Guild = '$Guild', GiveMembership = '$GiveMembership', War = '$War' WHERE id = $id"));
Also see #Rahul 's comment too. Your: "WHERE id='$id'" seems to be passing the wrong value. An ID is most likely not that version of a date/time. if you want to use the current date + time you could us something like NOW() It gives you the current date and time down to the second.

Related

when i update my database it wont update and the datas turns to 1.. literally 1

So I want to update my data in database, and I wanna make sure that the row it updates (which is the last name) is the same as the one logged in. I did is this:
'data' is the name of the table where the logged in last name is to be found, 'ict' is the table where the grades is in and another last name to so that i can compare if they're the same and update that row.
After I input the datas and confirm it, it says "Successfully Inserted" but the data doesn't get updated and it turns to 1.
This is my code:
<?php
include("sq.php");
$a21st = isset(($_POST['a21st']));
$a21st2 = isset(($_POST['a21st2']));
$entre = isset(($_POST['entre']));
$entre2 = isset(($_POST['entre2']));
$pe3 = isset(($_POST['pe3']));
$pe32 = isset(($_POST['pe32']));
$a1 = isset(($_POST['a1']));
$a12 = isset(($_POST['a12']));
$a2 = isset(($_POST['a2']));
$a22 = isset(($_POST['a22']));
$fil = isset(($_POST['fil']));
$fil2 = isset(($_POST['fil2']));
$mil = isset(($_POST['mil']));
$mil2 = isset(($_POST['mil2']));
$contem = isset(($_POST['contem']));
$contem2 = isset(($_POST['contem2']));
$physci = isset(($_POST['physci']));
$physci2 = isset(($_POST['physci2']));
$pe4 = isset(($_POST['pe4']));
$pe42 = isset(($_POST['pe42']));
$reaspro = isset(($_POST['reaspro']));
$reaspro2 = isset(($_POST['reaspro2']));
$a3 = isset(($_POST['a3']));
$a32 = isset(($_POST['a32']));
$a4 = isset(($_POST['a4']));
$a42 = isset(($_POST['a42']));
$immers = isset(($_POST['immers']));
$immers2 = isset(($_POST['immers2']));
if (mysqli_connect_error()) {
die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
} else {
$SELECT =("SELECT * FROM data");
$result = mysqli_query($db,$SELECT);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$last = $row['last'];
$UPDATE = "UPDATE `ict` SET a21st = $a21st, a21st2 = $a21st2, entre= $entre, entre2 = $entre2, pe3 = $pe3, pe32 = $pe32, a1 = $a1, a12 = $a12, a2 = $a2, a22 = $a22, fil = $fil, fil2 = $fil2, mil = $mil, mil2 = $mil2, contem = $contem, contem2 = $contem2, physci = $physci, physci2 = $physci2, pe4 = $pe4, pe42 = $pe42, reaspro = $reaspro, reaspro2 = $reaspro2, a3 = $a3, a32 = $a32, a4 = $a4, a42 = $a42, immers = $immers, immers2 = $immers2 WHERE last = '".$last."'";
if (mysqli_query($db,$UPDATE)){
$message = "Successfully Inputted!!";
echo "<script type='text/javascript'>alert('$message');</script>";
}else {
echo "Error: <br>". $db->error;
}
}
$db->close();$db
isset returns true if the value exists, which translates to 1 in a numeric context, which is probably not what you meant. Instead, you need to get the value itself. E.g.:
$a21st = isset($_POST['a21st']) ? $_POST['a21st'] : null;

PHP/SQL script stops unexpectedly, works in debug

I have a fairly long script that ran fine until last night (no changes made, that I know of).
The query that needs to be run is this:
$query = "INSERT INTO results SET
compId = ".$comp_id.",
eventId = ".$eventId.",
eventSession = '".$eventsession."',
driverId = ".$driverid.",
teamId = ".$teamid.",
position = ".$this->data['drivers']['position'][$i].",
raceTime = '".$racetime."',
laps = ".$this->data['drivers']['laps'][$i].",
pitstops = ".$this->data['drivers']['pitstops'][$i].",
engine = ".$engineId.",
tyres = '4',
sector1 = '".$sector1."',
sector2 = '".$sector2."',
sector3 = '".$sector3."',
fastestLap = '".$this->data['drivers']['bestTime'][$i]."',
grid = ".$grid;
// If debug is true then just var_dump but don't execute query
if ($this->debug) {
var_dump($query); echo '<br>';
}
else { // debug is false here, then not debug and run mysql query as usual
mysql_query($query);
}
I ran a debug/var_dump script that shows that all data is read correctly, and shows that the script correctly runs for each record in the dataset:
string(341) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 408, teamId = 142, position = 1, raceTime = '', laps = 9, pitstops = 0, engine = 57, tyres = '4', sector1 = '34.8867', sector2 = '35.8517', sector3 = '35.7191', fastestLap = '106.4575', grid = 0"
string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1910, teamId = 316, position = 2, raceTime = '', laps = 9, pitstops = 0, engine = 57, tyres = '4', sector1 = '35.0853', sector2 = '35.6669', sector3 = '35.7169', fastestLap = '106.4692', grid = 0"
string(343) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1934, teamId = 201, position = 3, raceTime = '', laps = 11, pitstops = 0, engine = 51, tyres = '4', sector1 = '35.1301', sector2 = '35.7618', sector3 = '35.7473', fastestLap = '106.6393', grid = 0"
string(343) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1263, teamId = 286, position = 4, raceTime = '', laps = 10, pitstops = 0, engine = 53, tyres = '4', sector1 = '35.0674', sector2 = '35.7036', sector3 = '35.9128', fastestLap = '106.6838', grid = 0"
string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1255, teamId = 286, position = 5, raceTime = '', laps = 8, pitstops = 3, engine = 53, tyres = '4', sector1 = '35.1663', sector2 = '35.9540', sector3 = '35.6342', fastestLap = '106.7544', grid = 0"
string(340) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1190, teamId = 8, position = 6, raceTime = '', laps = 3, pitstops = 0, engine = 51, tyres = '4', sector1 = '35.1284', sector2 = '35.8531', sector3 = '35.8777', fastestLap = '106.8593', grid = 0"
string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1092, teamId = 253, position = 7, raceTime = '', laps = 8, pitstops = 1, engine = 55, tyres = '4', sector1 = '35.2606', sector2 = '35.9344', sector3 = '35.7010', fastestLap = '106.8960', grid = 0"
string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 1393, teamId = 154, position = 8, raceTime = '', laps = 9, pitstops = 0, engine = 58, tyres = '4', sector1 = '35.1497', sector2 = '35.8700', sector3 = '36.0847', fastestLap = '107.1044', grid = 0"
string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 2048, teamId = 251, position = 9, raceTime = '', laps = 9, pitstops = 1, engine = 55, tyres = '4', sector1 = '35.2091', sector2 = '36.1934', sector3 = '35.8702', fastestLap = '107.2728', grid = 0"
string(343) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 621, teamId = 308, position = 10, raceTime = '', laps = 12, pitstops = 0, engine = 51, tyres = '4', sector1 = '35.2224', sector2 = '36.0565', sector3 = '36.3339', fastestLap = '107.6127', grid = 0"
string(342) "INSERT INTO results SET compId = 8, eventId = 934, eventSession = 'P', driverId = 447, teamId = 232, position = 11, raceTime = '', laps = 9, pitstops = 0, engine = 57, tyres = '4', sector1 = '35.1237', sector2 = '36.2297', sector3 = '36.2870', fastestLap = '107.6404', grid = 0"
However, when not in debug, this only runs for the first record in the set. No matter what dataset I feed into.
I am at a loss at explaining what is going wrong or if there is anything that stops the script after the first instance (which is entered correctly).
Note: the original code is not mine and I no longer have access to the original developer as he left our group, so I am just basing myself of the current information. As mentioned earlier, this script has worked correctly until last night and no changes have been made to other parts of the code that may affect it.
Thank you for any help!
EDIT: here is the whole loop, as requested in the comments:
private function queryDb()
{
$year=gmdate("Y");
// What $this->data holds at this point is an array with all drivers, positions, times, etc
// using ['driver'][0] => Jim Parisis that correspond to ['position'][0] => 1 and the ['bestTime'][0] => 95.9577 and so on
// Let's pick the event Id from $this->data
$eventId = $this->data['event'];
$eventsession = $this->data['evsess'];
// instantiate the finances class
// So we can use later any of its method like $finances->makeSomething($argument)
$finances = new Finances($eventId, $this->debug);
// Let's get the competition holding that event
$query = "SELECT competition FROM ".$this->dbName.".calendar WHERE id='$eventId'";
$result = mysql_query($query);
// $comp_id = mysql_fetch_row($result); // What you have there is an array array(1) { [0]=> string(1) "1" }
// That's true that competition is 1 but having in array you have to access it like $comp_id[0]
while ($row = mysql_fetch_row($result))
$comp_id = (int)$row[0]; // Better build it as simple integer to save memory
mysql_free_result($result);
// echo '<pre>'; print_r($this->data); echo '</pre>';
$i = 0; // Let's build some integer to play with because we have keys starting at 0 in the $this->data array
foreach ($this->data['drivers']['name'] as $key => $value) {
$driverFullName = $value; // now the foreach goes for each element of the 'drivers' key storing it in the $value variable
// Gets the users id
$query = "SELECT id FROM ".$this->dbName.".users WHERE CONCAT(firstName,' ',lastName) ='".$driverFullName."'";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result))
$driverid = $row[0];
mysql_free_result($result);
//var_dump($driverid);
// Gets the partA from contracts
$teamid = NULL;
if ($comp_id==10) { $teamid= 0; }
else {
$query = "SELECT partA FROM ".$this->dbName.".contracts WHERE partB ='".$driverid."' AND competition ='".$comp_id."' AND curState ='A'";
$result = mysql_query($query);
if (mysql_num_rows($result) != 0) {
while ($row = mysql_fetch_row($result))
$teamid = $row[0];
}
else { $teamid=0; echo "Warning: driver $driverFullName has no contract."; }
mysql_free_result($result);
}
//var_dump($teamid);
// Gets the engine
// We can wrap this and the next queries in just one by using LEFT JOIN but that's another story
$enginemodel = NULL;
$engineId = NULL;
/*$query = "SELECT te.engine as engineId, e.engine as engineModel, en.id as engineId2 FROM DCO_team_engines te LEFT JOIN DCO_engines e ON te.engine=e.id LEFT JOIN engines en ON en.brand=e.engine WHERE te.team ='".$teamid."' AND te.comp='".$comp_id."' AND te.year ='".$year."'";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result))
$engineid = $row[2];
mysql_free_result($result);*/
//Substitute engine
$query = "SELECT engine FROM ".$this->dbName.".DCO_team_engines WHERE team ='".$teamid."' AND comp ='".$comp_id."' AND year ='".$year."'";
$result = mysql_query($query);
if (mysql_num_rows($result) != 0) {
while ($row = mysql_fetch_row($result))
$engineId = $row[0];
}
mysql_free_result($result);
$query = "SELECT vehchoice FROM ".$this->dbName.".competitions WHERE id ='".$comp_id."'";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result))
$vehchoice = $row[0];
mysql_free_result($result);
if ($vehchoice==1) {
$query = "SELECT classes FROM ".$this->dbName.".licenses WHERE competition ='".$comp_id."' AND applicantId ='".$teamid."' AND concept ='2' AND validDate='".$year."'";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result))
$engineId = $row[0];
mysql_free_result($result);
}
//var_dump($engineId); echo '<br>';
// $this->data['sectors']['s1'][] = $sector1;
// $this->data['sectors']['s2'][] = $sector2;
// $this->data['sectors']['s3'][] = $sector3;
$sector1 = $this->data['sectors']['s1'][$i];
$sector2 = $this->data['sectors']['s2'][$i];
$sector3 = $this->data['sectors']['s3'][$i];
$laps = $this->data['drivers']['laps'][$i];
$position = $this->data['drivers']['position'][$i];
$finishstatus = $this->data['drivers']['finishStatus'][$i];
$untimed = $this->data['drivers']['untimed'][$i];
$carnumber = $this->data['drivers']['carnumber'][$i];
if ($finishstatus=='Finished Normally') { $racetime = $this->data['drivers']['finishTime'][$i]; }
if ($finishstatus=='DNF') { $racetime = "DNF"; }
$grid = 0; // grid is always empty until the following condition applies
if ($eventsession=='R' || $eventsession=='R1' || $eventsession=='R2' || $eventsession=='R3') {
$laprank = $this->data['drivers']['fastestlaprank'][$i];
$grid = $this->data['drivers']['gridpos'][$i];
}
if ($engineId == null)
$engineId = 'DEFAULT';
if ($comp_id=='12') { $carchosen = $this->data['drivers']['team'][$i];
$query = "SELECT id FROM ".$this->dbName.".available_vehicles WHERE label ='".$carchosen."'";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result))
$engineId = $row[0];
mysql_free_result($result);
}
$query = "INSERT INTO results SET
compId = ".$comp_id.",
eventId = ".$eventId.",
eventSession = '".$eventsession."',
driverId = ".$driverid.",
teamId = ".$teamid.",
position = ".$this->data['drivers']['position'][$i].",
raceTime = '".$racetime."',
laps = ".$this->data['drivers']['laps'][$i].",
pitstops = ".$this->data['drivers']['pitstops'][$i].",
engine = ".$engineId.",
tyres = '4',
sector1 = '".$sector1."',
sector2 = '".$sector2."',
sector3 = '".$sector3."',
fastestLap = '".$this->data['drivers']['bestTime'][$i]."',
grid = ".$grid;
// If debug is true then just var_dump but don't execute query
if ($this->debug) {
var_dump($query); echo '<br>';
}
else { // debug is false here, then not debug and run mysql query as usual
mysql_query($query);
}
// Ok, if you need to call fines method within the loop
// call it here, for example to insert rows depending on the driver values like laps, pitstops, etc.
// Notice we parse the whole array
$query = "SELECT careerLadder, usesManagement, vehchoice, monoEngine FROM ".$this->dbName.".competitions WHERE id='$comp_id'";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result)) {
$careerLadder = (int)$row[0]; // Better build it as simple integer to save memory
$usesManagement = (int)$row[1];
$vehchoice = (int)$row[2];
$monoEngine = (int)$row[3];
}
mysql_free_result($result);
if ($comp_id!=10)
{
if (($eventsession!='T')&&($eventsession!='P')&&($eventsession!='Q'))
{
$finances->standings($comp_id,$position, $driverid, $year, $teamid, $eventsession, $laps);
$finances->standingsarrange($comp_id);
}
}
if ($usesManagement==1)
{
$finances->ladderfinances($teamid, $comp_id, $eventsession, $position, $laprank,$laps);
}
if ($comp_id==1)
{
if ($eventsession=='T')
{
$finances->rdpoints($teamid,$comp_id,$laps,$untimed);
$finances->erdpoints($teamid,$comp_id,$laps,$untimed);
}
if (($eventsession!='T')&&($eventsession!='P'))
{
$finances->enginewear($driverid,$teamid,$laps,$carnumber);
}
}
$i++; // ok let's go to the next element if any, if foreach notices it's the last then it stops :)
}
$finances->standingstiebreak($comp_id);
//if ($dom->getElementsByTagName('Qualify')->item(0))
// $this->data['session'] = $dom->getElementsByTagName('Qualify')->item(0)->nodeName;
//if ($dom->getElementsByTagName('Practice1')->item(0))
// $this->data['session'] = $dom->getElementsByTagName('P')->item(0)->nodeName;
if ($eventsession=='T') { $session = "Test"; }
if ($eventsession=='P') { $session = "Practice"; }
if ($eventsession=='Q') { $session = "Qualifying"; }
if ($eventsession=="R") { $session = "Race"; }
if ($eventsession=='R1') { $session = "Race 1"; }
if ($eventsession=='R2') { $session = "Race 2"; }
if ($eventsession=='R3') { $session = "Race 3"; }
echo "<h2>$session results entered successfully</h2>";
Issue resolved - the DB in which we were trying to add the rows had the ID field set to smallint. We had literally reached the limit of rows (65535) and therefore it was working on debug and not in real life.

Insert Multiple not empty

I need to insert multple records i have this right now.
Its adds also the empty field because i dont know how to exclude them from inserting:
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
/* as_factuurregel */
$product1 = $_POST['product1'];
$product2 = $_POST['product2'];
$product3 = $_POST['product3'];
$product4 = $_POST['product4'];
$product5 = $_POST['product5'];
$product6 = $_POST['product6'];
$aantal1 = $_POST['aantal1'];
$aantal2 = $_POST['aantal2'];
$aantal3 = $_POST['aantal3'];
$aantal4 = $_POST['aantal4'];
$aantal5 = $_POST['aantal5'];
$aantal6 = $_POST['aantal6'];
$sql = "INSERT INTO as_factuurregel (productid, factuurid, aantal)
VALUES ('$product1', 'test', '$aantal1'),('$product2', 'test', '$aantal2'),('$product4', 'test', '$aantal3'),('$product5', 'test', '$aantal5'),('$product5', 'test', '$aantal5')";
Thanks for helping.
Greetings
Try this:
$product1 = $_POST['product1'];
$product2 = $_POST['product2'];
$product3 = $_POST['product3'];
$product4 = $_POST['product4'];
$product5 = $_POST['product5'];
$product6 = $_POST['product6'];
$array_one = array($product1,$product2,$product3,$product4,$product5,$product6);
$aantal1 = $_POST['aantal1'];
$aantal2 = $_POST['aantal2'];
$aantal3 = $_POST['aantal3'];
$aantal4 = $_POST['aantal4'];
$aantal5 = $_POST['aantal5'];
$aantal6 = $_POST['aantal6'];
$array_two = array($aantal1,$aantal2,$aantal3,$aantal4,$aantal5,$aantal6);
$newArray = array();
foreach ($array_one as $key => $value) {
if($value != '' && $array_two[$key] != '')
{
$newArray[] = "('$value','test','$array_two[$key]')";
}
}
$values = implode(",",$newArray);
$sql = "INSERT INTO as_factuurregel (productid, factuurid, aantal)
VALUES ".$values;
You can optimize this solution if you optimize your input fields and move them in an array.

Unknown column 'E5V7D24M10Y2015' in 'where clause'

I have the code below to update (more 'add to') a row in a database. I have read a few very similar posts, but still can't see where I'm going wrong...
I am getting the error:
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 ''EventID' = '2' WHERE GameID = 'E2V1D24M10Y2015' AND PlayerID = '55'' at line 1
In this case, 'E5V7D24M10Y2015' is the value of $GameID. I am looking for a column called GameID where the value is E5V7D24M10Y2015, not a column of that name. Please tell me why my $sql is looking for a column named after the value it is looking for.
Each time the function runs the count($Runners) will be different and the values in each variable will be different. That is why I have the SQL in a loop.
if ($formtype == "gameresults"){
$Runners = $_POST['runners'];
$event = $_POST['event'];
$eid = $_POST['eid'];
$vid = $_POST['vid'];
$GameID = $_POST['GameID'];
$date = $_POST['date'];
$season = $_POST['season'];
$region = $_POST['region'];
$notes = $_POST['notes'];
$kev = "kev#email.com";
$email = $_POST['manager'];
$notes = wordwrap($notes,70);
$subject = ("Results for " . $event);
$tix = "";
$cashs = "";
for ($x = 1; $x < ($Runners + 1); $x++){
$ID = $_POST['ID' . $x];
$Name = $_POST['Name' .$x];
$Place = $_POST['Place'.$x];
$Points = $_POST['Points'.$x];
$Cash = $_POST['Cash'.$x];
$Ticket = $_POST['Ticket'.$x];
$vn = $_POST['vn'];
$buyin = $_POST['buyin'];
$data = array($eid,$vid,$region,$buyin,$GameID,$date,$season,$ID,$Name,$Place,$Points,$Ticket,$Cash,$Runners);
$fields = array('EventID','VenueID','Region','Buyin','GameID','Date','Season','PlayerID','Name','Position','Points','Ticket?','Cash','Runners');
for ($x = 0; $x < (count($data) - 1); $x++){
$sql = "UPDATE results SET '$fields[$x]' = '$data[$x]' WHERE GameID = '$GameID' AND PlayerID = '$ID'";
$res = mysqli_query($dbcon, $sql) or die("Update failed. <br>" . mysqli_error($dbcon));
}
You could modify your code to be more clear. I did example but I did not map all the fields so you need to adjust it.
$eid = 4;
$vid = 5;
$region = 'aa';
$buyin = 'asd';
$gameID = 5;
$date = 5;
$playerID = 10;
$fields = array(
'EventID' => $eid,
'VenueID' => $vid,
'Region' => $region,
'Buyin' => $buyin,
'GameID' => $gameID,
'PlayerID'=> $playerID,
'Date' => $date,
);
$numItems = count($fields);
$query = "UPDATE `results` SET ";
$i = 0;
foreach($fields as $name => $value) {
++$i;
if($name == 'GameID' || $name == 'PlayerID') {
continue;
}
$query .= sprintf(" `%s` = '%s'%s ", $name, $value, ($i === $numItems ? "": ","));
}
$query .= sprintf(" WHERE `GameID` = '%d' AND `PlayerID` = '%d'", $fields['GameID'], $fields['PlayerID']);
echo $query;
Fields can be maped via $key => $value and then used in foreach loop. Also using sprintf() makes is more clear to read. However, the best option would be using prepared statements.
Also you don't need to make multiple UPDATE queries, just SET more parameters in one query.
$data = array($eid,$vid,$region,$buyin,$GameID,$date,$season,$ID,$Name,$Place,$Points,$Ticket,$Cash,$Runners);
$fields = array('EventID','VenueID','Region','Buyin','GameID','Date','Season','PlayerID','Name','Position','Points','Ticket?','Cash','Runners');
for ($x = 0; $x < (count($data) - 1); $x++){
$sql = "UPDATE results SET " . $fields[$x] . " = '$data[$x]' WHERE results.GameID = $GameID AND results.PlayerID = $ID";
$res = mysqli_query($dbcon, $sql) or die("Update failed. <br>" . mysqli_error($dbcon));
}
Try this by specifying alias.

web service update error

i have a web service that show me this error
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 '0', date_de_naissance = '1988-02-02', lieu_de_naissance = ' at line 4{"items":[["succes"]]}
<?php
include('settings.php');
mysql_connect($host,$user,$password);
mysql_select_db($base);
mysql_query('SET CHARACTER SET utf8');
$id_patient = $_GET['id_patient'];
$nom = $_GET['nom'];
$prenom = $_GET['prenom'];
$Sexe = $_GET['Sexe'];
$date_de_naissance = $_GET['date_de_naissance'];
$lieu_de_naissance = $_GET['lieu_de_naissance'];
$adresse = $_GET['adresse'];
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
$telephone = $_GET['telephone'];
$email = $_GET['email'];
$situation_familiale = $_GET['situation_familiale'];
$profession = $_GET['profession'];
$numero_securite_sociale = $_GET['numero_securite_sociale'];
$taille = $_GET['taille'];
$poids = $_GET['poids'];
$groupe_sanguin = $_GET['groupe_sanguin'];
$allergies = $_GET['allergies'];
$antecedents_chirurgicaux = $_GET['antecedents_chirurgicaux'];
$antecedents_medicaux = $_GET['antecedents_medicaux'];
$antecedents_familiaux = $_GET['antecedents_familiaux'];
if ($id_patient!= NULL )
{
$req = "UPDATE patient SET nom = '".$nom."' , prenom = '".$prenom.", Sexe = '".$Sexe."',
date_de_naissance = '".$date_de_naissance."',
lieu_de_naissance = '".$lieu_de_naissance."',
adresse = '".$adresse."',
latitude = '".$latitude."',
longitude = '".$longitude."',
telephone = '".$telephone."',
email = '".$email."',
situation_familiale = '".$situation_familiale."',
profession = '".$profession."',
numero_securite_sociale = '".$numero_securite_sociale."',
taille = '".$taille."',
poids = '".$poids."',
groupe_sanguin = '".$groupe_sanguin."',
allergies = '".$allergies."',
antecedents_chirurgicaux = '".$antecedents_chirurgicaux."',
antecedents_medicaux = '".$antecedents_medicaux."',
antecedents_familiaux = '".$antecedents_familiaux."'
WHERE id_patient = '".$id_patient."' ";
$sql=mysql_query($req);
echo mysql_error();
$items = array("items" => NULL);
$items["items"][] = array("succes");
echo json_encode($items,JSON_UNESCAPED_UNICODE);
}
else{$items = array("items" => NULL);
$items["items"][] = array("erreur");
echo json_encode($items,JSON_UNESCAPED_UNICODE);}
?>
A closing quote was missing.
$req = "UPDATE patient SET nom = '".$nom."' , prenom = '".$prenom."', Sexe = '".$Sexe."',

Categories