I am updating a row in the mysql table. After updating the row, I want to show the user the information such as "This information is updated now"
SO this is my code:
$result->$mysqli->query("UPDATE table SET status ="updated" WHERE id = '$id'");
if($result->num_rows > 0) {
///I WANT TO ECHO/ALERT THE UPDATE INFORMATION HERE
}
Thanks in advance:
No, its not ->num_rows since you didn't select any rows so it doesn't make sense to know how many rows did it yield. If you wan't to know if it did make an update on a row, use affected_rows:
$sql = "UPDATE table SET status = 'updated' WHERE id = ?";
$update = $mysqli->prepare($sql);
$update->bind_param('s', $id);
$update->execute();
if($update->affected_rows > 0) {
echo 'yeah it updated that row!';
}
MySQLi query with update query returns FALSE on failure and TRUE on success.
So, basically, if $result is true then it has been updated. You can just echo "Success".
$result = $mysqli->query("UPDATE table SET status ="updated" WHERE id = '$id'");
if ($result)
{
echo "Success";
}
If you need to write which data has been changed you need to add SELECT queries before and\or after UPDATE query and output it.
You can show the number of updated rows for example:
$result->$mysqli->query("UPDATE table SET status ="updated" WHERE id = '$id'");
if($result->num_rows > 0) {
printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows);
}
http://php.net/manual/es/mysqli.affected-rows.php
Related
I am looping through the rows of the database with fetch_assoc() and I am selecting two columns of it. I check the values of these two columns of each row. If they are between 2 values then I updated a third row as 1(TRUE) if a statement is true. I am creating two connections because there are two statements, the one that selects the information and the other that updates the column that I want to be updated. When I try to print the information in the screen it seems that the SELECT statement works but when I try to UPDATE the column that I want the UPDATE statement does not update the database. Here is my code:
$conn= mysqli_connect("localhost","root","root");
$variablech = 1 ;
$query = "SELECT Client, Info FROM DataTable";
if ($result = mysqli_query($conn, $query)) {
while($row=mysqli_fetch_row($result)) {
if((($row['Client']>=54.055) && ($row['Client']<=54.117) ) && (( $row['Info']>=-4.827) && ( $row['Info']<=-4.317)))
{
$variablech = 0;
$sql= " UPDATE DataTable SET InfoData='$variablech' WHERE Client='".$row['Client']."' AND Info='".$row['Info']."'";
$conn->query($sql);
echo "yes";
}
else
{
$variablech = 1;
$sql= " UPDATE DataTable SET InfoData='$variablech' WHERE Client='".$row['Client']."' AND Info'".$row['Info']."'";
$conn->query($sql);
echo "no";
}
}
}
This should do the trick:
...
$sql= "UPDATE DataTable SET InfoData='".$variablech."' WHERE Client='".$row['Client']."' AND Info='".$row['Info']."'";
mysqli_query($conn, $sql)
...
Suggestion:
Not sure, if you have an auto_increment field in your table. If you do, then select it in select query and then use the same to update the data in update query. That will speed up the database stuff and so is your project.
I want to have a row with a column called: 'seen', then this column will have either a default of no value or a 'yes' value.
So..
Grab one row where it hasn't been 'seen' before.
Update the above row 'seen column' to say 'yes'.
If all rows have the value of 'yes' then a notice/error displays:
You have successfully completed all numbers.
I've tried the best I can do achieve it, but it's not working. I think my logic in tackling this may be incorrect?
include 'DB.php';
$con = mysqli_connect($host,$user,$pass);
$dbs = mysqli_select_db($databaseName, $con);
// Grabs one row where it hasn't been seen before
$query = mysqli_query("SELECT number, association, image_file, skeleton, sound, colour, comments FROM num_image WHERE seen='' ORDER by rand() LIMIT 1");
// Updates the above row with the 'seen' column saying 'yes''
$query = mysqli_query("UPDATE num_image SET seen = yes");
// Fetches Result
$thestuff = mysqli_fetch_row($query);
$seenme=$_POST['seen']; // get value of 'seen' column
$result = mysqli_query("SELECT * FROM num_image where seen=$seenme");
// Trying to delivery a message if the enitre 'seen' column is ALL yes.
while($row = mysqli_fetch_row($result))
{
if($row['seen'] == 'yes')
{ // All numbers seen
echo 'You have successfully completed all numbers.';
echo json_encode($thestuff);
}
else
{ // Show numbers
echo json_encode($thestuff);
}
}
Does the SELECT and UPDATE row also have to be an if statement?
Cheers
You have to escape the value in the Update sentence:
$query = mysqli_query("UPDATE num_image SET seen = 'yes' ");
I have one drop down and lists are aMan, bMan, cMan.I am selecting any one of them from drop down. So whatever I am selecting from drop down I want to update that records according to list. Below update query is updating all my records because i added '$action_points' for each.
For example. If I selected bMan from the drop down then in update table will update only bMan records according to user_id.If I select aMan then update table it will update only aMan with 10.It will not effect on other.
I am getting the issue on update query.Would you help me with update query?
$result = $conn->query($sql_user);
if (isset($result->num_rows) > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$action_type=$row['action_type'];// Value will be aMan,bMan, cMan
$action_points=$row['action_points']; //10, 20, 30
}
}
$sql = "UPDATE man SET aMan='$action_points',bMan='$action_points', cMan='$action_points' where user_id='$user_id'";
$result = $conn->query($sql);
Update table
You are selecting from select drop down it means it will pass the value, that you have either aMan, bMan or cMan.
so you can do it like this,
$action_type = $_GET['action_type'];
$sql = "update man set `$action_type` = '$action_value' where id = $user_id";
Above is an example.
Firstly, you should replace if (isset($result -> num_rows) >0 ) with if(isset($result)) && ($result->num_rows>0)) .The first condition returns the number of rows (which at the least is 0) and then checks if it is set. Thus, isset will always return true, even when $result is not set. The second condition solves this problem
You have the type of list to update, why don't you use it?
For eg:
$result = $conn->query($sql_user);
if(isset($result)) && ($result->num_rows>0)) {
// output data of each row
while($row = $result->fetch_assoc()) {
$action_type=$row['action_type'];// Value will be aMan,bMan, cMan
$action_points=$row['action_points']; //10, 20, 30
}
}
$sql = "UPDATE man SET $action_type = $action_points WHERE user_id='$user_id'";
$result = $conn->query($sql);
This shall automatically update the required column
U should use AND in your query
UPDATE man SET aMan='$action_points' AND bMan='$action_points' AND cMan='$action_points' where user_id='$user_id'"
Or use several update query it means once update aMan row then a query for bMan row and so on.
The issue is because you are updating the three column at a time, you have to make it conditional like:
$action_type=$row['action_type'];// Value will be aMan,bMan, cMan
$action_points=$row['action_points']; //10, 20, 30
$column = '';
if($action_type == 'aMan'){
$column = 'aMan';
}
else if($action_type == 'bMan'){
$column = 'bMan';
}
else if($action_type == 'cMan'){
$column = 'cMan';
}
$sql = "UPDATE man SET ".$column." = '".$action_points."' where user_id='$user_id'";
I want to ask about inserting a record into db like if we give the id in url throught $Getn and check if the data of id n is present in table then it must update else it must insert the data ?
how can we do it in PHP plz help me
you could do something like
<?php
$n = $_GET["n"];
$query = "SELECT COUNT(*) AS numberOfRecords FROM table WHERE id = '$n'";
mysql_query($query);
if ($numberOfRecords == 0)
mysql_query("INSERT INTO [...]");
else
mysql_query("UPDATE [...]");
This may help you. Simply check id is set or not
if(isset($_GET['n']) && !empty($_GET['n'])){
//your update query is here
}else{
//your insert query is here
}
The below script inputs data to a database this takes some information from a form then stores them in to the database. And I'm also using uplodify to upload a image file and store the file name in the database but my issue is this data processing script keeps updating the row ID one never jumps to the second line I tried every thing can some one help me with this or show me what I'm doing wrong.
Also this checks the ID and if it's not equal to 1 then does an insertion if it's equal then update it but this not happening.
The ID is auto incrementing.
My script
<?php
/**
* #author SiNUX
* #copyright 2013
*/
include ('connect.php');
$getId = mysql_query("SELECT ID FROM poiinfo ORDER BY ID DESC LIMIT 1");
$row = mysql_fetch_array($getId);
$poiName = $_REQUEST['Name'];
$poiDes = $_REQUEST['Descrip'];
$poiCon = $_REQUEST['ConInfo'];
//$poiId = $_REQUEST['pID'];
if($row['ID'] != "1"){
$dbData = "INSERT INTO poiinfo(`Name`, `Des.`, `Contact`) VALUES ('$poiName','$poiDes','$poiCon')";
$putData = mysql_query($dbData);
if ($putData){
echo "Data inserted";
}else {
echo "Not Done";
}
}else {
$updLn = "UPDATE `poiinfo` SET `Name`='$poiName',`Des.`='$poiDes',`Contact`='$poiCon'";
$updDone = mysql_query($updLn);
if ($updDone){
echo "Data inserted";
}else {
echo "Not Done";
}
}
?>
I tried u r suggestions but it's still the same now my code for the update is looks like this.
$updLn = "UPDATE `poiinfo` SET `Name`='$poiName',`Des.`='$poiDes',`Contact`='$poiCon' WHERE `ID`='".$row['ID']."'";
But still it keeps up dating the ID 1 not moving on to the next one.
Your update query is missing a WHERE clause. Try this:
$updLn = "UPDATE `poiinfo` SET `Name`='$poiName',`Des.`='$poiDes',`Contact`='$poiCon' WHERE ID = '".$row['ID']."'";
Also be beware of MySQL Injections: http://en.wikipedia.org/wiki/SQL_injection
To check why your update failed, you should call mysql_error in your last else clause :
} else {
echo mysql_error();
}
As for the first problem : if you never insert a new record (I don't see how that could happen, provided your code), you will never have a record whose ID is 2.
$updLn = "UPDATE `poiinfo` SET `Name`='$poiName',`Des.`='$poiDes',`Contact`='$poiCon'";
You need a where clause in this sql to specify a record to update. Currently it is updating all records.
$updLn = "UPDATE `poiinfo` SET `Name`='$poiName',`Des.`='$poiDes',`Contact`='$poiCon' WHERE `ID` = ".$row['id']";";
You will need to set an $id variable for this to work.