At the moment I am running an update query which is working, however it allows null values to be inserted which I don't want.
mysql_query('UPDATE community_table SET
age = "'.$age.'",
gender = "'.$gender.'",
pregnant = "'.$pregnant.'",
diet = "'.$diet.'",
smoker = "'.$smoker.'",
alcohol = "'.$alcohol.'",
exercise = "'.$exercise.'",
bmi = "'.$bmi.'",
sleeping = "'.$sleeping.'",
stress = "'.$stress.'",
education = "'.$education.'"
WHERE username = "' . $_SESSION['user'] . '" ');
I tried this query which seems to prevent null values being entered, however it also prevents values being entered when the variables are not null.
age = "'.$age.'",
gender = "'.$gender.'",
pregnant = "'.$pregnant.'",
diet = "'.$diet.'",
smoker = "'.$smoker.'",
alcohol = "'.$alcohol.'",
exercise = "'.$exercise.'",
bmi = "'.$bmi.'",
sleeping = "'.$sleeping.'",
stress = "'.$stress.'",
education = "'.$education.'"
WHERE age IS NOT NULL,
gender IS NOT NULL,
pregnant IS NOT NULL,
diet IS NOT NULL,
smoker IS NOT NULL,
alcohol IS NOT NULL,
exercise IS NOT NULL,
bmi IS NOT NULL,
sleeping IS NOT NULL,
stress IS NOT NULL,
education IS NOT NULL and where
username = "' . $_SESSION['user'] . '" ');
Is there something wrong with the above query, or do I perhaps have a null value in my input?
Thanks.
You can prevent empty string from being sent from your script or try this
UPDATE community_table SET
education = IFNULL($education, education) -- if null, maintain previous value
Just check values before insert like this -
UPDATE community_table SET
education = ifnull($education, education)
Related
I want to check if a row value from my database is NULL or empty so I did the following:
$existcheck = "
SELECT *
FROM snm_content";
$existcheckcon = $conn->query($existcheck);
$existcheck = $existcheckcon->fetch_assoc();
if((!empty($existcheck['plaats'])) AND (!empty($existcheck['straat']))){
echo 'vol';
}else{
$vultable = "
ALTER TABLE snm_content
ADD (straat varchar(255) NULL,
plaats varchar(255) NULL)";
$vultablecon = $conn->query($vultable);
$vultable = $vultablecon->fetch_assoc();
$updatetable = "
UPDATE snm_content
SET straat = '".$straatnaam."', plaats = '".$bedrijven['plaats']."'";
$updatetablecon = $conn->query($updatetable);
$updatetable = $updatetablecon->fetch_assoc();
}
But I get the following message:
There was an error running the query [Duplicate column name 'straat']
Both straat and plaats in snm_content are all filled with NULL.
So why does it get to the else statement if the values are NULL?
I should mention, this code is inside a while loop from another query.
You should check if the column exist not if column have a data so you have to do like this :
$existcheck = "DESC snm_content";
$existcheckcon = $conn->query($existcheck);
$existcheck = $existcheckcon->fetch_assoc();
if((!empty($existcheck['plaats'])) AND (!empty($existcheck['straat']))){
echo 'vol';
}else{
$vultable = "
ALTER TABLE snm_content
ADD (straat varchar(255) NULL,
plaats varchar(255) NULL)";
$vultablecon = $conn->query($vultable);
$vultable = $vultablecon->fetch_assoc();
$updatetable = "
UPDATE snm_content
SET straat = '".$straatnaam."', plaats = '".$bedrijven['plaats']."'";
$updatetablecon = $conn->query($updatetable);
$updatetable = $updatetablecon->fetch_assoc();
}
I really hate that error message, since it is the most useless error message in the history of man.
Anyhow I think I have stared at this VERY simple sql for an hour and still come up blank as to where it believes the problem is. Hope someone can help me or maybe some rubber ducking will do the trick.
The php code:
$sql = "UPDATE events SET titel = '$this->estart',
endTime = '$this->eend',
desc = '$this->desc',
dd = '$this->dDmed',
dato = '$this->dato',
ticketId = '$this->ticket' WHERE id = $this->id";
And the SQL error thrown:
right syntax to use near 'desc = '2222222222222222',
dd = 'shop',
dato = '2015-01-14' at line 3[ UPDATE events SET titel = '08:30:00',
endTime = '09:00:00',
desc = '2222222222222222',
dd = 'shop',
dato = '2015-01-14',
ticketId = '2222222222222' WHERE id = 4]' in ....
the table layout:
id int(11)
titel varchar(200)
navn varchar(200)
email varchar(255)
tlf varchar(20)
domæne varchar(150)
kundNumb int(14)
abnId varchar(15)
startTime time
endTime time
desc text
ticketId varchar(20)
dd varchar(5)
dato date
Hope someone can assist, I am sick and tired of that sql statement.
The problem is with the desc field. DESC is a keyword in the SQL syntax. So you will have to quote desc with back-ticks like this `desc`.
UPDATE events SET
`titel` = '$this->estart',
`endTime` = '$this->eend',
`desc` = '$this->desc',
`dd` = '$this->dDmed',
`dato` = '$this->dato',
`ticketId` = '$this->ticket'
WHERE id = $this->id
You missed a comma:
$sql = "UPDATE events SET titel = '$this->estart',
endTime = '$this->eend',
desc = '$this->desc',
dd = '$this->dDmed',
dato = '$this->dato',
ticketId = '$this->ticket' WHERE id = $this->id";
I believe changing
ticketId = '$this->ticket'
to
ticketId = $this->ticket
should work..
Seems Error is in ticket_ID i think you are using ticket_id as number type or integer type and passing it as string '$this->ticket' remove '' and keep it as $this->ticket
you miss a comma, and some " and '. :
"UPDATE events SET titel = '".$this->estart."',
endTime = '".$this->eend."',
desc = '".$this->desc."',
dd = '".$this->dDmed."',
dato = '".$this->dato."',
ticketId = '".$this->ticket."' WHERE id = ".$this->id.";
I have an UPDATE query and using Ajax, I wanted to know if any value is empty can I only update the values that not empty in the database. I don't know if this is possible to have a if statement or something to check to skip the empty values. I know I can just add another form element but just wanted to know if there was another solution.
Only if the data is POST from front end form. If data not POST don't update this Title = '.$title .',
$id = $_POST['id'];
$title = "";
$description = $_POST['Description'];
$date = $_POST['Date'];
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = '.$title .',
`Description` = '.$description.',
`Date` = '.$date =.'
WHERE `id` = '.$id;
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
Update: This is what worked for me. Thanks Karim Daraf
$query = " UPDATE user SET
Title = Coalesce($title,Title ) etc...
Try it with Coalesce .
$query = " UPDATE user
SET
`Title` = CASE WHEN `Title`='' or `Title` IS NULL THEN '$title' END,
`Description` = CASE WHEN `Description`='' Or `Description` IS NULL THEN '$description' END,
`Date` = CASE WHEN `Date`='' Or Date` IS NULL THEN '$date' END
WHERE `id` = '".$id."' ";
or :
$query = " UPDATE user
SET
`id` = Coalesce('$id''".$id."' , NULLIF(`id`,'')),
`Title` = Coalesce('$title''".$title."',NULLIF(`Title`,'') ) ,
`Description` = Coalesce('$description''".$description."' , NULLIF(`Description`,'') ) ,
`Date` = Coalesce('$date''".$date."',NULLIF(`Date`,''))
WHERE `id` = '$id''".$id."' ";
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = COALESCE(NULLIF("'.$title.'", ""),`Title`),
`Description` = "'.$description.'",
`Date` = "'.$date.'"
WHERE `id` = "'.$id.'"';
Not sure to understand: you have data and want to update, but only if some fied in the DB are empty?
In the case perfom only a where:
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = '.$title .',
`Description` = '.$description.',
`Date` = '.$date =.'
WHERE `id` = '.$id.' AND Title = '';
for example
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";
I am PDO newbie, and i can't figure why i can select only first row in Table on database.
This is my DataBase TABLE :
Column Type Null Default Comments
id int(11) No
cred varchar(20) No
tok char(40) No
ptok char(40) No
t char(128) No
expires varchar(26) No
Indexes
Keyname Type Unique Packed Column Cardinality Collation Null Comment
PRIMARY BTREE Yes No id 1 A No
ptok BTREE Yes No ptok 1 A No
And this is my SELECT (find) function :
public function findTriplet($credential,$token, $persistentToken) {
$sql = "SELECT IF(SHA1(?) = {$this->tokenColumn}, 1, -1) AS token_match " .
"FROM {$this->tableName} WHERE {$this->credentialColumn} = ? " .
"AND {$this->persistentTokenColumn} = SHA1(?) LIMIT 1 ";
$query = $this->connection->prepare($sql);
$query->execute(array($token, $credential, $persistentToken));
$result = $query->fetchColumn();
if(!$result) {
return self::TRIPLET_NOT_FOUND;
}
elseif ($result == 1) {
return self::TRIPLET_FOUND;
}
else {
return self::TRIPLET_INVALID;
}
}
Anyway i tryed to search for answer , but i dont know PDO so good so its not matter..
I tryed play with that , no succses..
Anyone know what is my problem in the findTriplet function and what i am doing wrong ?
It will only select first database row so if i have more then 1 row's it will return false.
Thanks allot.
Remove "LIMIT 1" from your variable and see if results change:
$sql = "SELECT IF(SHA1(?) = {$this->tokenColumn}, 1, -1) AS token_match " .
"FROM {$this->tableName} WHERE {$this->credentialColumn} = ? " .
"AND {$this->persistentTokenColumn} = SHA1(?) LIMIT 1 ";
Becomes:
$sql = "SELECT IF(SHA1(?) = {$this->tokenColumn}, 1, -1) AS token_match " .
"FROM {$this->tableName} WHERE {$this->credentialColumn} = ? " .
"AND {$this->persistentTokenColumn} = SHA1(?)";
It appears you are retrieving a match, so you are seeing a SINGLE result. I'm not sure of the application, but you might want to keep a Limit in the SQL statement. Removing the LIMIT will at least be a good testing point.