add quotations in mysql table via php - php

$sql_images = "'".$uploaded_image."', '".$uploaded_image2."', '".$uploaded_image3."', '".$uploaded_image4."', '".$uploaded_image5."'";
$db->query("UPDATE menu SET nav_name = $navigation, image_bg = $sql_images WHERE id = $id;") or die(mysql_error());
I cant update my table and I know that the problem is with the $sql_images variable because I have added quotations and have messed it all up, but that is how I want and need it, is there any way to add that variable in the table without any problem? because right now it gives me an error.

Try it like this
$sql_images = "\'".$uploaded_image."\', \'".$uploaded_image2."\', \'".$uploaded_image3."\', \'".$uploaded_image4."\', \'".$uploaded_image5."\'";
$db->query("UPDATE menu SET nav_name = '$navigation', image_bg = '$sql_images' WHERE id = $id;") or die(mysql_error());

Related

How to make a MySQL column value equal a variable in PHP

I have in my database a column called counter, i'm trying to save one of the rows of that column into a variable in PHP and make it equal the column rating. I do not want to make it from sql directly, I need to make it through php.
The problem is that when I run the query, the value for rating does not change at all. Please give me a solution
$cntrvalue = "SELECT counter FROM schools WHERE name = 'School1'";
$cntrResult = $this->conn->query($cntrvalue);
$mys1 = "UPDATE schools SET rating = $cntrResult WHERE name = 'School1'";
$this->conn->query($mys1);
Thanks
EDIT:
Sorry the code above gives an error, this is the code that changes nothing
$cntrvalue = "SELECT counter FROM schools WHERE name = 'School1'";
$this->conn->query($cntrvalue);
$mys1 = "UPDATE schools SET rating = $cntrvalue WHERE name = 'School1'";
$this->conn->query($mys1);
EDIT 2:
What i'm trying to do now is that i'm trying to get the value of a SUM query:
$mys4 = "SELECT SUM(`s1`+`s2`*2 +`s3`*3 + `s4`*4 + `s5`*5) FROM schools WHERE name = 'School1'";
$mys4Result = $this->conn->query($mys4);
$mys4Value = $mys4Result->fetch_assoc()[''];
The thing is that there is no column in the db to fetch from for this operation. What am I supposed to do? Thanks
Want you want to achieve, can be done within one mysql query, but if you just want how to fix that code:
You need to use the $cntrResult outside the string. Try string concatenation.
First you need to make sure, you have the correct value, as $cntrResult is not an string object, its an resultset:
$value = $result->fetch_assoc()['counter']
And the query:
"UPDATE schools SET rating = '" . $value . "' WHERE name = 'School1'";

two query updates doesn't work

hi guys I got a little problem while I am was trying to updates my table.
I just explain you my situation:
index.php
<?php
$query = mysql_query("SELECT * FROM `references` ORDER BY `ID` ASC") or die(mysql_error());
while($referenc = mysql_fetch_assoc($query)){
echo '
<tr>
<td>'.$referenc['name'].'</td>
<td>'.$referenc['url'].'</td>
<td>'.$referenc['date'].'</td>
<td>up</td>
<td>down</td>
<td>edit</td>
</tr>
';
}
?>
how you can see I have there two links for up and down.
after click on one of that link I am going on updatetest.php
updaterest.php
f.e
move = up
position = 2;
id = 2;
<?php
$move = $_GET['move'];
if($move == "up"){
$getcurrentid = $_GET['id'];
$moveup = $_GET['position']-1;
$getprevposition = $_GET['position']-1;
$position = $_GET['position'];
$query = mysql_query("UPDATE `references` SET `position` = '$moveup' WHERE `references`.`ID`='$getcurrentid'") or die(mysql_error());
$query2 = mysql_query("UPDATE `references` SET `position` = '$position' WHERE `references`.`position`='$getprevposition'") or die(mysql_error());
}
else{echo'down';}
?>
Now it doing always just one update if I comment first update second one is working fine if I comment second update first one is working fine but if I uncomment it and wanna do it together its always doing just one update. I looking for some way how to do 2 updates in same time
any ideas ?
You need to do the queries in another order. $moveup and $getprevposition have the same value, so you are currently doing something like this
change position to new where id = x
change position to old where position = new
Since you just changed position to new, the second WHERE will catch the item you already edited (and undo your edit). Instead, you should do it like this
change position to old where position = new
change position to new where id = x
Now you don't have a conflict, as the row you really want to change doesn't yet have position = new.

Data is not entered in the table of my database

I have to enter the audi in a column of a particular for a given user (in long blob).
The code is this
$audi = addslashes(file_get_contents($_FILES['audi']['tmp_name']));
$audi_na = addslashes($_FILES['audi']['name']);
$tab=$_SESSION['email'];
mysql_query("UPDATE `database`.`TableOfUsers` SET `audio` = '$audi' AND `audiname` = '$audi_name' WHERE WHERE `user`.`email` = '$tab'") or die(mysql_error());
but my data is not being stored in the table..... something else get stored in the table with size 1 byte (always) but not required data.
I am beginner so pardon me if I am asking a silly question.
Lots of wrong in your query...correct them
mysql_query("UPDATE `database`.`TableOfUsers`
SET `audio` = '$audi', `audiname` = '$audi_name'
WHERE `user`.`email` = '$tab'") or die(mysql_error());
You have added WHERE 2times and for multiple column update you can separate them with comma
You are using where 2 times in query. Make your query like this
mysql_query("UPDATE `database`.`TableOfUsers` SET `audio` = '$audi' AND `audiname` = '$audi_name' WHERE `user`.`email` = '$tab'") or die(mysql_error());
If you have mysql_error(), you should know the error
In your query there is a syntax error. There is WHERE repeated two times. And also when you have multiple column updates you have to separate with them with , not with AND i hope it will work.
mysql_query("UPDATE `database`.`TableOfUsers`
SET `audio` = '$audi',
`audiname` = '$audi_name'
WHERE `user`.`email` = '$tab'
") or die(mysql_error());

MySQL table row will not UPDATE

I am trying to update a column in a row in a MySQL table. The column is the 'votes' column and when someone submits an HTML form there is a hidden input with a value of "1" that gets submit and posted. This is the code I am using to try to update the vote count:
if(isset($_POST['image_id']) && isset($_POST['vote'])){
$image_id = $mysqli->real_escape_string($_POST['image_id']);
$vote = $mysqli->real_escape_string($_POST['vote']);
$sql_users_vote = "SELECT * FROM users WHERE id='$image_id'";
$result_users_vote = $mysqli->query($sql_users_vote);
$row_vote = mysqli_fetch_array($result_users_vote);
$votes_count = $row_vote['votes'];
$new_votes = $votes_count + $vote;
$sql_vote = "UPDATE users WHERE id='$image_id' SET votes=$new_votes";
$result_vote = $mysqli->query($sql_vote);
}
I have echo'ed out the variable up until $sql_vote and $image_id, $vote, $votes_count and $new_votes all echo out the correct values. I'm guessing that there is a problem in the UPDATE syntax. I've checked it over and over but can't seem to find anything. I know that I don't have quotes around $new_votes in the UPDATE because I believe that is correct syntax. I've tried it with quotes and it doesn't work that way either.
Can someone help me identify the problem? Thanks!
Doesn't the SET come before the WHERE?
$sql_vote = "UPDATE users SET votes = $new_votes WHERE id = '$image_id'"
Or does it not matter?
$sql_vote = "UPDATE users SET votes=$new_votes WHERE id='$image_id'";

Change values around in mysql database using php

I have the code below:
$do_id=$_GET['id'];
$new_workorder=$_GET['workorder'];
//get current workorder on file for record that will be updated
$query = "SELECT workorder FROM jvltodos WHERE id = '$do_id' LIMIT 1";
$bla = mysql_query($query);
$workorder_old = mysql_fetch_row($bla);
//get record for workorder on file that will have the new workorder
$query = "SELECT id FROM jvltodos WHERE workorder = '$new_workorder' LIMIT 1";
$bla = mysql_query($query);
$id_fix = mysql_fetch_row($bla);
if (isset($new_workorder) && $new_workorder!=$workorder_old) {
//update the changed record
$query = "UPDATE jvltodos
SET workorder={$new_workorder}
WHERE id = {$do_id}";
$result = mysql_query($query);
//update the record that had the old workorder
$query = "UPDATE jvltodos
SET workorder={$workorder_old}
WHERE id = {$id_fix}";
$result = mysql_query($query);
}
I pass the 'id' and 'workorder' into the url, so I GET them first. With that I update the record with that 'id' to the new 'workorder'. So far so good.
But I want to find the record that has that 'workorder' and update that with the 'workorder' from the old 'id'. (In simple terms: when I increase/decrease the 'workorder', I want to switch two records around)
To me it looks pretty straightforward, but it doesn't work. The first thing (update the id with the new workorder works.. but the other record stays the same.. it keeps the workorder that was already in there)
What am I missing? (must be something simple that I'm just overlooking.)
#andrewsi Thank you for the suggestion! I took the code out, copied it over to another page and did some echo's to see what was going on and that way I figured out easily what the problem was.
Example:
$bla = mysql_query($query);
$workorder_old = mysql_fetch_row($bla);
The $workorder_old that I wanted to have should've been a number. However, this didn't happen, but returned.. an array.
When I changed the code to...
$bla = mysql_query($query);
$bla = mysql_fetch_row($bla);
$workorder_old = $bla[0];
... it worked like a charm.
tiny note: I'm so used to grabbing the data in 1 step (another language), that I forgot that I needed to make a few steps in PHP. Guess it's time to throw it in a function perhaps.. :-)

Categories