I have the following code:
if(isset($_POST['regKitsForm'])){
$kitsiteID = $_POST['kitsiteID'];
$sql = "SELECT patientID FROM patient WHERE patientNum=".$_POST['kitpatientID'];
$connect->execute($sql);
$get = $connect->fetch();
$kitpatientID = $get[0];
if(is_numeric($_POST['kitNum1'])) {
$kitNum1 = str_pad($_POST['kitNum1'], 5, '0', STR_PAD_LEFT);
$kitForm = $_POST['kitForm'];
$sql = "UPDATE form$kitForm SET v0".$kitForm."_dd_kitNum1=$kitNum1 WHERE patientID = $kitpatientID AND siteID = $kitsiteID";
This should be inputing e.g.: 00001 from $kitNum1, but it isn't... it's just inputing 1.
Please help
M
Make sure, that your database column is of a string type like varchar(5) and not of an integer type. In addition, put quotes around the value in your query so that it isn't interpreted as a number, but as a string instead:
$sql = "UPDATE form$kitForm SET v0".$kitForm."_dd_kitNum1='$kitNum1' WHERE patientID = $kitpatientID AND siteID = $kitsiteID";
Related
I am trying to do an update query in php to update my database but the query is not working. It is probably something simple.
$query = "UPDATE Events
SET charity_name = '$charity_name' ,
charity_reg = $charity_reg ,
Event_Name = '$event_tit',
Event_Status_Code = '$event_stat',
Start_Date = $event_dat,
Hours = $event_hour,
location = '$event_loc',
Other_Details = $event_content,
event_image = $imageData,
image_name = '$imageName',
max_available_spaces = $event_spaces,
Event_type = '$eve_category',
event_cost = $event_cost,
event_organiser = '$event_organiser'
WHERE Event_ID = $the_event_id";
You are not putting quotes ('') around some values, that might be a problem unless all thoses values are boolean/ints. Make sure to put quotes around all values, like '$imageData' instead of $imageData Also watch out for sql injections when you are directly inputting the values in your query. Better to use prepared statements
$query = "UPDATE Events
SET charity_name = '$charity_name' ,
charity_reg = '$charity_reg' ,
Event_Name = '$event_tit',
Event_Status_Code = '$event_stat',
Start_Date = '$event_dat',
Hours = '$event_hour',
location = '$event_loc',
Other_Details = '$event_content',
event_image = '$imageData',
image_name = '$imageName',
max_available_spaces = '$event_spaces',
Event_type = '$eve_category',
event_cost = '$event_cost',
event_organiser = '$event_organiser'
WHERE Event_ID = $the_event_id;";
EDIT: as #dWinder mentioned: if $the_event_id is not an integer, make sure to also put quotes around that value.
I am trying to create a check-in/check-out table in my database. My check-in form works without issue, inserting the time into my database. The problem occurs when I try to check out. Everything is good on the first entry...
But when I try to check in and check out again, this happens...
So far so good, but when I check out...
Currently, my code updates the out column and totalTime column of all matching child_id's.
Here is my code:
// Select the correct child from the database
$sql_childID = "SELECT id FROM child
WHERE firstName = '$childFirstName'
AND lastName = '$childLastName'";
$result = $pdo->query($sql_childID);
$row = $result->fetch();
$var = $row['id'];
// Insert the check out time for the child
$query = "UPDATE checkinout
SET `out` = :nowTime
WHERE child_id = $var
AND `in` IS NOT NULL";
$statement = $pdo->prepare($query);
$statement->bindValue(':nowTime', date("YmjHis"));
$statement->execute();
// Select check in time for specified child
$sql_inTime = "SELECT `in` FROM checkinout
WHERE child_id = $var";
$inResult = $pdo->query($sql_inTime);
$inRow = $inResult->fetch();
$inTime = strtotime($inRow['in']);
// Select the check out time for specified child
$sql_outTime = "SELECT `out` FROM checkinout
WHERE child_id = $var";
$outResult = $pdo->query($sql_outTime);
$outRow = $outResult->fetch();
$outTime = strtotime($outRow['out']);
// Find total hours
$totalTime = abs($outTime - $inTime)/(60*60);
// Update totalHours column for specified child
$queryTotalTime = "UPDATE checkinout
SET totalTime = :totalTime
WHERE child_id = $var
AND 'out' IS NOT NULL";
$statement = $pdo->prepare($queryTotalTime);
$statement->bindValue(':totalTime', $totalTime);
$statement->execute();
I think you could do all of this in your first update statement using TIMESTAMPDIFF rather than figuring the total time with PHP:
UPDATE checkinout
SET
out = NOW(),
totalTime = TIMESTAMPDIFF(SECOND, `in`, NOW()) / 3600
WHERE
child_id = $var
AND out IS NULL
The criteria WHERE out IS NULL will only update rows that do not have a value in the out column yet.
IF you have MySQL Db THEN sql will be
SELECT TIMESTAMPDIFF(HOUR,in,out) from checkinout;
how i can update mysql table with php?
example
i have:
$sql = mysql_query("UPDATE table_name SET numbers = 'null' where no = '1'") or die(mysql_error());
$sql = mysql_query("UPDATE table_name SET numbers = 'null' where no = '2'") or die(mysql_error());
$sql = mysql_query("UPDATE table_name SET numbers = 'null' where no = '3'") or die(mysql_error());
i need one mysql request, i try this example but it doesn't work.
$sql = mysql_query("UPDATE table_name SET numbers = 'null' WHERE no IN ('1, 2, 3')") or die(mysql_error());
The solution you have come up with only has one item in the IN, and so would be equivalent to:
UPDATE table_name SET numbers = 'null' WHERE no = '1, 2, 3'
You need to use separate strings for each value, i.e.:
UPDATE table_name SET numbers = 'null' WHERE no IN ('1', '2', '3')
If you remove the single quotes around your WHERE no IN ('1, 2, 3') it will fix your problem
This is assuming that your no column is an integer column
I am sending data from an android app to save in my mysql database.
My Codes are
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = $time, transaction_status = 2 , remark = $rem WHERE transaction_id = $t_id LIMIT 1");
And my dabase is as,
remark varchar(500) latin1_general_ci.
The problem is whenever I have $rem as a numbric value then database is updated but if I use any text then it wont.
Please help me.
Make sure wrapping string data with single quote.
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = '$time', transaction_status = 2 , remark = '$rem' WHERE transaction_id = $t_id LIMIT 1")
Text should be wrapped by single quotes: ''
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = $time, transaction_status = 2 , remark = '$rem' WHERE transaction_id = $t_id LIMIT 1");
$saa = "update aspirantdt set vote = 'vote'+1 where 'post_id' = '$id' ";
When i check the database the value didnt increase.
Help me out.
yuo have a sintax mistake in your update query, you are using quotest instead of backtick. Use backticks for column names and quotes for values try to change as follow
$saa = "update `aspirantdt` set `vote` = (`vote`+1) where `post_id` = '".$id."' ";
Just remove quotes or use Backtics for column names.
$saa = "UPDATE aspirantdt SET vote = vote + 1 where post_id = '$id' ";
Or with backticks
$saa = "UPDATE `aspirantdt` SET `vote` = `vote` + 1 where `post_id` = '$id' ";
$saa = "update aspirantdt set vote = 'vote'+1 where 'post_id' = '$id' ";
means that 'vote' and 'post_id' are literal strings, not table names (that is, it will compare $id to the actual string post_id instead of the value of the post_id column).
What you want is backticks to quote them as a column/table name instead;
$saa = "update `aspirantdt` set `vote` = `vote`+1 where `post_id` = '$id' ";
You don't need quotes around vote as its a column name, not a string.
Your SQL is probably trying to put vote1 in an int column.