Whenever I execute an update-query, my whole table is updated. What do I have to do when I just want ONE value to be updated?
Here is my database structure:
ID || photo || sequence
1 || test.png || 1
2 || bla.png || 2
Whenever I execute this script,
if (isset($_POST['submitted'])) {
$project = new Project();
$project->sequence = $_POST['sequence'][$key];
$projectid = $_POST['photoid'];
if($project->updateProject($_DB, $projectid)) {
$feedback = "OK";
} else {
$feedback = "NOT OK";
}
}
Results in this:
ID || photo || sequence
1 || || 4
2 || || 2
So, what do I have to do to just update the sequence-value in the database without touching the rest of the data in the database...
FUNCTION:
public function updateProject($db, $id) {
$sql = "UPDATE tblProject SET
sequence = '".$db->escape($this->sequence)."'
WHERE id = '".$id."'";
return $db->insert($sql);
}
INSERT FUNCTION:
public function insert($sql) {
mysql_query($sql, $this->_connection);
return mysql_affected_rows($this->_connection);
}
There must be a problem with your $project->updateProject() function.
Try with simple query:
$qry = "UPDATE tblProject SET sequence = '".$project->sequence."'
WHERE ID =".(int)$projectid.";
mysql_query($qry);
Basically, whenever you get this problem, you are updating your table without any WHERE clause. Like the code below:
UPDATE myTable SET myField = 'newValue';
In this case, all of your stored records will update with the new value.
Use a WHERE clause in your query to update just one or some specified records.
UPDATE myTable SET myField = 'newValue' WHERE tableId = 'yourId';
Related
i am trying to insert into multi table after select query return 0 (not found raws) select query working and insert query never done when submite "displayid" and there is no any syntax error
code:
<?php
if ($_POST["displayid"] == TRUE) {
$sqlid = "SELECT * FROM doc1 WHERE idnum ='$pidnum' AND stats='$ok'";
$result = mysqli_query($conn, $sqlid);
if (mysqli_num_rows($result) > 0) {
$sqlup = "UPDATE doc1 SET m_phone='$pm_phone', seen='$dataseen' WHERE idnum ='$pidnum'";
mysqli_query($conn, $sqlup);
$found = 1;
} else {
$found = 0;
$sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date)
VALUES ('$pfname','$plname','$ptname','$pfuname','$pidnum','$pm_phone','$todaydate')";
$conn->query($sqlfail)
}
}
?>
Use this code:
$sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date)
VALUES ('".$pfname."','".$plname."','".$ptname."','".$pfuname."','".$pidnum."','".$pm_phone."','".$todaydate."')";
make similar changes for update command as well
you actually have one error
$conn->query($sqlfail)
should be
$conn->query($sqlfail);
AND stats='$ok'";
i can't see a variable with this name i think you mean AND stats='ok'";
I have a form with two radio buttons 'phone me' and 'collect keys'. In order for the user to only select one of the two options, the input name are the same for both. If the user selects the first radio button where the value is equal to yes, then update the below database field:
UPDATE survey_bookings_mainsite SET appointment_phone = 'Yes' WHERE ref = $ref
else if the second radio button where the value is equal to no, then update the below database field:
UPDATE survey_bookings_mainsite SET appointment_keys = 'Yes' WHERE ref = $ref
Issue:
The second query returns 'No' in the appointment_phone field and displays NULL in the appointment_keys field. What is supposed to happen is the appointment_phone field to display 'No' and the appointment_keys field to also display 'Yes'. I have tried to in the second IF-Statement to re-assign $appointment_phone to equal to look like this:
$appointment_phone = mysql_real_escape_string($_POST['appointment_keys']);
but that doesn't work.
include("config/cn.php");
if(isset($_POST['Submit'])){
// Use array_map to secure all POST values:
$_POST = array_map('mysql_real_escape_string', $_POST);
$ref = $_POST['ref'];
$property_number = $_POST['property_number'];
$property_address1 = $_POST['property_address1'];
$property_address2 = $_POST['property_address2'];
$property_town = $_POST['property_town'];
$property_postcode = $_POST['property_postcode'];
$appointment_phone = $_POST['appointment_phone'];
$appointment_contact_number = $_POST['appointment_contact_number'];
$appointment_keys = $_POST['appointment_keys'];
if($_POST['appointment_phone'] == 'Yes'){
$sql = mysql_query("UPDATE survey_bookings_mainsite SET appointment_phone = 'Yes' WHERE ref = $ref");
}
if ($_POST['appointment_phone'] == 'No'){
$sql = mysql_query("UPDATE survey_bookings_mainsite SET appointment_keys = 'Yes' WHERE ref = $ref");
}
$collect_number = $_POST['collect_number'];
$collect_postcode = $_POST['collect_postcode'];
$collect_address1 = $_POST['collect_address1'];
$collect_address2 = $_POST['collect_address2'];
$collect_town = $_POST['collect_town'];
$collect_phone = $_POST['collect_phone'];
$report_name = $_POST['report_name'];
$report_number = $_POST['report_number'];
$report_address1 = $_POST['report_address1'];
$report_address2 = $_POST['report_address2'];
$report_town = $_POST['report_town'];
$report_postcode = $_POST['report_postcode'];
$report_phone = $_POST['report_phone'];
$report_email = $_POST['report_email'];
$special_instructions = $_POST['special_instructions'];
$enter_sql = "INSERT INTO survey_bookings_mainsite (ref,property_number,property_address1,property_address2,property_town,property_postcode,appointment_phone,appointment_contact_number,collect_number,
collect_address1,collect_address2,collect_town,collect_postcode,collect_phone,report_name,report_number,report_address1,report_address2,report_town,
report_postcode,report_phone,report_email,special_instructions)
VALUES(\"$ref\",\"$property_number\",\"$property_address1\",\"$property_address2\",\"$property_town\",
\"$property_postcode\",\"$appointment_phone\",\"$appointment_contact_number\",\"$collect_number\",\"$collect_address1\",\"$collect_address2\",\"$collect_town\",\"$collect_postcode\",
\"$collect_phone\",\"$report_name\",\"$report_number\",\"$report_address1\",\"$report_address2\",\"$report_town\",\"$report_postcode\",\"$report_phone\",\"$report_email\",\"$special_instructions\")";
$enter_query = mysql_query($enter_sql);
header('Location: /thankyou.php');
exit;
}
Your problem is that your two if-statements will never run both, one will work on yes and one will work on no. You have to combine them into 1 query:
$query = "UPDATE survey_bookings_mainsite SET
appointment_phone = '".($_POST['appointment_phone']=='Yes' ? 'Yes' : 'No')."' ,
appointment_keys = '".($_POST['appointment_phone']=='No' ? 'Yes' : 'No')."'
WHERE ref = $ref LIMIT 1";
$sql = mysql_query($query);
I took the liberty of adding LIMIT 1, if you only have to update 1 line, this will increase speed significantly when there is more load :)
The code in the query is short if/else, or ternairy. The following does exactly the same
if( $var === true ){ echo 'yes';}
else{ echo 'No';}
echo $var ===true ? 'yes' : 'no';
You method could actually work (though I strongly recommend you don't do the following!):
// Same code, simplefied for example:
if($_POST['appointment_phone'] == 'Yes'){
$sql = "UPDATE table SET appointment_phone = 'Yes',appointment_keys='No'";
}
if ($_POST['appointment_phone'] == 'No'){
$sql = "UPDATE table SET appointment_phone = 'No',appointment_keys='Yes'";
}
You dont want this because now you have two queries doing almost the exact same thing. What if you want to switch to 1/0 instead of yes/no? You would have to switch two queries (twice the room for error).
Just imagine what would happen if you have 10 inputs like this.
I'm trying to write a query to check which column to update. The user sends an action which they performed (a like or a comment) and I'm trying to update a table. Is it possible to check inside the query which column to update? For example:
DB structure:
id imageName imageLikesCount imageCommentsCount
$actionPerformed = "like";
mysqli_query($link, "UPDATE table (if $actionPerformed=like SET imageLikesCount+1
else imageCommentsCount+1)
WHERE imageName='$image'");
I'm not sure how to phrase that, if it's possible at all. Any advice? Thanks in advance!
though meverhart913 has a way to do it, the better way to do the same thing is to instantiate your variable based on the if condition, then just plug that variable into your string. This keeps you from having to repeat your string over and over as well as allows you to easily add additional conditions.
if($actionPerformed=="like"){
$col = imageLikesCount;
else{
$col = imageCommentsCount;
}
mysqli_query($link, "Update table SET '$col' = '$col + 1' where imageName = '$image'");
if($actionPerformed=="like"){
mysqli_query($link, "Update table SET imageLikesCount = imageLikesCount + 1 where imageName = '$image'");
}
else {
mysqli_query($link, "Update table SET imageCommentsCount = imageCommentsCount + 1 where imageName = '$image'");
}
I'm not a php programmer so my syntax won't be correct, but here are two ways to do it:
if ($actionPerformed == "like")
query for updating imageLikesCount
else if ($actionPerformed == "comment")
query for updating imageCommentsCount
else
whatever
Or
if ($actionPerformed == "like")
$column = "imageLikesCount";
else ($actionPerformed == "comment")
$column = "imageCommentsCount";
$sql = "update table set $column = $column + 1";
Then execute it.
I am using if else condition with foreach loop to check and insert new tags.
but both the conditions(if and alse) are being applied at the same time irrespective of wether the mysql found id is equal or not equal to the foreach posted ID. Plz help
$new_tags = $_POST['new_tags']; //forget the mysl security for the time being
foreach ($new_tags as $fnew_tags)
{
$sqlq = mysqli_query($db3->connection, "select * from o4_tags limit 1");
while($rowq = mysqli_fetch_array($sqlq)) {
$id = $rowq['id'];
if($id == $fnew_tags) { //if ID of the tag is matched then do not insert the new tags but only add the user refrence to that ID
mysqli_query($db3->connection, "insert into user_interests(uid,tag_name,exp_tags) values('$session->userid','$fnew_tags','1')");
}
else
{ //if ID of the tag is not matched then insert the new tags as well as add the user refrence to that ID
$r = mysqli_query($db3->connection, "insert into o4_tags(tag_name,ug_tags,exp_tags) values('$fnew_tags','1','1')");
$mid_ne = mysqli_insert_id($db3->connection);
mysqli_query($db3->connection, "insert into user_interests(uid,tag_name,exp_tags) values('$session->userid','$mid_ne','1')");
}
}
}
i think you are inserting
$r = mysqli_query($db3->connection, "insert into o4_tags(tag_name,ug_tags,exp_tags)
values('$fnew_tags','1','1')");$mid_ne = mysqli_insert_id($db3->connection);
and then you are using while($rowq = mysqli_fetch_array($sqlq))
which now has records you just inserted therefore your if is executed
I'm pretty sure the select query below will always return the same record.
$sqlq = mysqli_query($db3->connection, "select * from o4_tags limit 1");
I think most of the time it will goes to the else, which execute the 2 insert.
Shouldn't you write the query like below?
select * from o4_tags where id = $fnew_tags limit 1
say I have a variable
$id = mt_rand();
how can I query the mysql database to see if the variable exists in the row id, if it does exist then change the variable $id, once the variable is unique to all other stored ids, then insert it into the database?
Thanks you guys.
$con = mysql_connect("<host>","<login>","<pass>");
if ($con) {
mysql_select_db('<schemata>', $con);
$found = false;
while (!$found) {
$idIamSearching = mt_rand();
$query = mysql_query("SELECT count(*) FROM <table> WHERE <idColumnName>='".$idIamSearching."'");
$result = mysql_fetch_row($query);
if ($result[0] > 0) {
mysql_query("INSERT INTO <table> (<column>) VALUES ('".$idIamSearching."')");
$found = true;
}
}
mysql_close($con);
}
Your description is hard to understand, so, this is something that could give you pointers...
'SELECT COUNT(*) as count from table where row_id="'.$variable.'" LIMIT 1'
make sure to escape the variable if it's user input or if it's going to have more than alphanumeric characters
then fetch the row and check if count is 1 or greater than 0
if one, then it exists and try again (in a loop)
although, auto increment on the id field would allow you to avoid this step
$bExists = 0;
while(!$bExists){
// Randomly generate id variable
$result = mysql_query("SELECT * FROM table WHERE id=$id");
if($result){
if(mysql_num_rows($result) > 0){
$bExists = 1;
} else {
// Insert into database
$bExists = 1;
}
}
1 Randomly generate id variable
2 Query database for it
2.1 Result? exit
2.2 No result? Insert