I want to implement this query :
if(x=1){
$update = "close = '$date'";
}
else {
$update = "open = '$date'";
}
$query = "Update table1 set $update where id=100";
mysql_query($query);
but I got an error, the Mysql can't execute the query ?
<?php
if($x==1){
$update = "close = '".$date."'";
}
else {
$update = "open = '".$date."'";
}
$query = "update table1 set $update where id=100";
mysql_query($query);
?>
use this
<?php
if(x==1){
$update = "close = '$date'";
}
else {
$update = "open = '$date'";
}
$query = "Update table1 set '".$update."' where id=100";
mysql_query($query);
?>
try to use this code........
<?php
if($x==1){ // use $ for variables
$update = "close = '".$date."' "; //always concatenate variables
}
else {
$update = "open = '".$date."' ";
}
$query = "Update table1 set '".$update."' where id=100";
mysql_query($query, $connection); // don`t forget to add mysql connection
?>
Put your query in the quotes. Try following :
if(x==1){
$update = "close='".$date."'";
}
else {
$update = "open = '".$date."'";
}
$query = "Update table1 set ".$update." where id=100";
mysql_query($query) or die(mysql_error());
Put mysql_error() to check what error are you getting from mysql
Replace
$query = Update table1 set $update where id=100;
to
$query = "Update table1 set ".$update." where id=100";
Related
What I want to do is a select query and then if it is true set a variable of a table called 'afiliadospadron' to 1 but if it is false (it do not return anything) just set the variable to 0. My code isn't working. Can somebody help me please?
$query= "select nrodoc from oct20 where nrodoc = 05463xx union select nrodoc from dic20 where nrodoc = 054631xx";
$query2= "update afiliadospadron set condVotar = '1' where nroDoc = '5463xxx' ";
//$query3= "UPDATE afiliadospadron SET condVotar = 0 WHERE nroDoc = '5463xxx'";
if ($query) {
$query2;
} else {
echo "next time";
// $query3 = "UPDATE afiliadospadron SET condVotar = 0 WHERE nroDoc = '5463192'";;
}
$statement = $conexion->prepare($query);
$statement = $conexion->prepare($query2);
//$statement = $conexion->prepare($query3);
$statement->execute();
try it
if ($result = $mysqli -> query($query)) {
$query2;
// Free result set
$result -> free_result();
}
code from: https://www.w3schools.com/php/func_mysqli_query.asp
The answer was
$statement = $conexion->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
if ($result = $result ) {
echo "yas";
$statement = $conexion ->prepare($query2);
$statement->execute();
// Free result set
} else {
echo "Wasnt found";
$statement = $conexion ->prepare($query3);
$statement->execute();
}
Is there a way to write this bunch of if statements shorter?
if($data==name){
$sql1 = "UPDATE people set firstname = ? WHERE id = '$id'";
$q = $pdo->prepare($sql1);
$q->execute(array($name));
}
if($data==age){
$sql2 = "UPDATE people set age = ? WHERE id = '$id'";
$q = $pdo->prepare($sql2);
$q->execute(array($age));
}
if($data==color1){
$sql3 = "UPDATE people set paint1 = ? WHERE id = '$id'";
$q = $pdo->prepare($sql3);
$q->execute(array($color));
}
if($data==color2){
$sql4 = "UPDATE people set paint2 = ? WHERE id = '$id'";
$q = $pdo->prepare($sql4);
$q->execute(array($color));
}
if($data==color3){
$sql5 = "UPDATE people set paint3 = ? WHERE id = '$id'";
$q = $pdo->prepare($sql5);
$q->execute(array($color));
}
I tried to make a function
function data($info, $information, $name){
if($data==$info){
$sql = "UPDATE people set $information = ? WHERE id = '$id'";
$q = $pdo->prepare($sql);
$q->execute(array($name));
}
}
data(name, firstname, $name);
But it is not working like this, I do not get a result. Maybe because of the variable inside the sql request?
Php switch, from the docs:
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}
// same as
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
Should be what you are looking for. http://php.net/manual/en/control-structures.switch.php
<?php
switch($data){
case "name":
$field = "firstName";
break;
case "age":
$field = "age";
break;
case "color1":
$field = "paint1";
break;
case "color2":
$field = "paint2";
break;
case "color3":
$field = "paint3";
break;
default:
echo 'Invalid selection';
exit;
break;
}
$sql = "UPDATE people set $field = ? WHERE id = '$id'";
$q = $pdo->prepare($sql);
$q->execute(array($name));
try to use if elseifbecause if you use if statement for each one it taking long time. so when first condition true means no need to check another if condition so use if elseif statement. or use switch case
if($data==name){
$sql1 = "UPDATE people set firstname = ? WHERE id = '$id'";
$q = $pdo->prepare($sql1);
$q->execute(array($name));
}
else if($data==age){
$sql2 = "UPDATE people set age = ? WHERE id = '$id'";
$q = $pdo->prepare($sql2);
$q->execute(array($age));
}
else if($data==color1){
$sql3 = "UPDATE people set paint1 = ? WHERE id = '$id'";
$q = $pdo->prepare($sql3);
$q->execute(array($color));
}
else if($data==color2){
$sql4 = "UPDATE people set paint2 = ? WHERE id = '$id'";
$q = $pdo->prepare($sql4);
$q->execute(array($color));
}
else if($data==color3){
$sql5 = "UPDATE people set paint3 = ? WHERE id = '$id'";
$q = $pdo->prepare($sql5);
$q->execute(array($color));
}
else
{
echo "nothing";
}
(OR)
switch ($data) {
case name:
$sql1 = "UPDATE people set firstname = ? WHERE id = '$id'";
$q = $pdo->prepare($sql1);
$q->execute(array($name));
break;
case age:
$sql2 = "UPDATE people set age = ? WHERE id = '$id'";
$q = $pdo->prepare($sql2);
$q->execute(array($age));
break;
case color1:
$sql3 = "UPDATE people set paint1 = ? WHERE id = '$id'";
$q = $pdo->prepare($sql3);
$q->execute(array($color));
break;
case color2:
$sql4 = "UPDATE people set paint2 = ? WHERE id = '$id'";
$q = $pdo->prepare($sql4);
$q->execute(array($color));
break;
case color3:
$sql5 = "UPDATE people set paint3 = ? WHERE id = '$id'";
$q = $pdo->prepare($sql5);
$q->execute(array($color));
break;
}
I recently was fiddling with my code and ran into an issue and i cannot figure out where it is. I added the "Update site_sync SET test = test+1" code last, which is firing perfectly fine. but everything else seems to be catching somewhere and it's not throwing errors anywhere on my site.
function skynetInfect($db)
{
$sql = "SELECT skyNet FROM site_sync;";
$sql .= "UPDATE site_sync SET test = test+1;";
$sql .= "SELECT count(*) FROM starinformation WHERE starOwner = -1;";
$que = $db->prepare($sql);
try { $que->execute();
$que->nextRowset();
while($row = $que->fetch(PDO::FETCH_BOTH))
{
if($row[0] == 'on')
{
$que->nextRowset();
$row2 = $que->fetch(PDO::FETCH_BOTH);
$x = $row2[0];
echo $x;
$sql = "UPDATE starinformation SET starOwner = -1 WHERE starOwner <> -1 ORDER BY rand() LIMIT {$x};";
$que = $db->prepare($sql);
$que->bindParam('id', $rand);
try{ $que->execute();}catch(PDOException $e) { echo $e->getMessage();}
}
}
}catch(PDOExceptions $e) { echo $e->getMessage();}
}
You are overwriting $que inside the inner if conditional. That happens here:
$sql = "UPDATE starinformation SET starOwner = -1 WHERE starOwner <> -1 ORDER BY rand() LIMIT {$x};";
$que = $db->prepare($sql); // use a variable name other than $que here
Also, is there any reason you are actually using PDO::FETCH_BOTH?
$sql = "SELECT skyNet FROM site_sync;";
$sql .= "UPDATE site_sync SET test = test+1;";
$sql .= "SELECT count(*) FROM starinformation WHERE starOwner = -1;";
$que = $db->prepare($sql);
I don't think you can execute multiple queries using PDO
$que = $db->prepare($sql);
I don't see any parameter in your Query so why the prepare?
what's the error in update command ?? it doesn't update the database ?
if($action=="new")
{
$query1="insert into activity_basic_info(activity_id) values($activity_id)";
$result1=mysql_query($query1);
if($result1)
echo "01";
else
echo "00";
}
else
{
$query="update activity_basic_info set";
if(isset($name))
$query = $query." name='$name',";
if($describtion!="" && describtion!=NULL)
$query = $query." describtion='$describtion',";
if($city!=NULL&&$city!="")
$query = $query." city_id=$city,";
if($region!=NULL)
$query = $query." region_id=$region,";
if($street!=NULL)
$query = $query." street_id=$street,";
if($telephone!="")
$query = $query." telephone=$telephone,";
if($email!=NULL&&$email!="")
$query = $query." email='$email',";
if($url!=NULL&&$url!="")
$query = $query." url='$url'";
else
$query = $query."url=''";
$query = $query." where activity_id=$activity_id";
$result=mysql_query($query);
echo $query;
}
?>
You should check the success of $result = mysql_query($query); then if false, check the value of mysql_error() http://php.net/manual/en/function.mysql-error.php
This query runs when I comment out the code above '}else{'. Have done so many like it and way more complex but I can't see where I'm tripping up.
(The page won't run like a syntax mistake. all vars match table)
Thanks.
Allen
if ($version == 'prem'){
$sql ="SELECT * FROM artistInfo WHERE user_id = '$user_id' AND artist_name = '$artist_name' ";
$res = mysql_query($sql);
$num = mysql_num_rows($res);
if($num>0){
while($row = mysql_fetch_array($res)){
$artist_id = $row['artist_id'];
} else {
mysql_query("INSERT INTO artistInfo (user_id, artist_name) VALUES ('$user_id', '$artist_name')");
$row_num = mysql_insert_id();
$artist_id = $user_id."-".$row_num;
mysql_query("UPDATE artistInfo SET artist_id = '$artist_id' WHERE row_num = '$row_num' ");
}
}
}
You're missing the closing } for the while loop.
missing } for while loop
if($num>0){
while($row = mysql_fetch_array($res)){
$artist_id = $row['artist_id'];
}
}
else {
I think that your last bracket got misplaced. you will also need to move the last } just before else
if ($version == 'prem'){
$sql ="SELECT * FROM artistInfo WHERE user_id = '$user_id' AND artist_name = '$artist_name' ";
$res = mysql_query($sql);
$num = mysql_num_rows($res);
if($num>0){
while($row = mysql_fetch_array($res)){
$artist_id = $row['artist_id'];
}
} else {
mysql_query("INSERT INTO artistInfo (user_id, artist_name) VALUES ('$user_id', '$artist_name')");
$row_num = mysql_insert_id();
$artist_id = $user_id."-".$row_num;
mysql_query("UPDATE artistInfo SET artist_id = '$artist_id' WHERE row_num = '$row_num' ");
}
}