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
Related
I'm a learner of PHP. This commit and rollback process does the task at each line. How can I commit and rollback all at once?
<?php
$con = mysql_connect('localhost','user', 'abcdefg');
if(!con)
{
echo "Can't connect to db.";
}else {
mysql_select_db('test');
}
if(!file_exits('test.csv')
{
echo "Can't find the file.";
}
$ar_1 = file('test.csv', FILE_IGNORE_NEW_LINES);
foreach ($ar_1 as $ar1)
{
$test = explode(",", $ar1);
$sql = "SELECT * FROM DB WHERE CD = '$test[0]'";
$result = mysql_query_($sql);
$sql = "INSERT INTO DB(CODE) VALUES ('$test[0]');";
$result = mysql_query($sql);
if($result === true)
{
//Commit
$sql = "commit";
mysql_query($sql);
echo "Committed";
}else {
//Rollback
$sql = "rollback";
mysql_query($sql);
echo "Rollback";
}
}
mysql_close($con);
?>
you need to commit out of the loop, i used $bool to see if there somewhere the $result isn't true
it will look something like :
edit:
breaking from the loop when something is wrong as you don't need to continue looping to other elements.
$bool = 1;
foreach ($ar_1 as $ar1)
{
$test = explode(",", $ar1);
$sql = "SELECT * FROM DB WHERE CD = '$test[0]'";
$result = mysql_query_($sql);
$sql = "INSERT INTO DB(CODE) VALUES ('$test[0]');";
$result = mysql_query($sql);
if($result === false)
{
//Rollback
$bool = 0;
$sql = "rollback";
mysql_query($sql);
echo "Rollback";
break;
}
}
if($bool == 1)
{
//Commit
$sql = "commit";
mysql_query($sql);
echo "Committed";
}
mysql_close($con);
I create a function to find all firstname and lastname in my database all I want if that data is already exist I just want to output, error message
my question is how to create a function to check if data is already exist?
this is my function to find all data of firstname and lastname.
function find_student_by_firstname($firstname){
global $con;
$safe_firstname = prep($firstname);
$sql = "SELECT * ";
$sql .= "FROM studeprofile ";
$sql .= "WHERE FirstName = '{$safe_firstname}' ";
$sql .= "LIMIT 1";
$student_set = mysqli_query($con, $sql);
confirm_query($student_set);
if($student = mysqli_fetch_assoc($student_set)){
return $student;
} else {
return null;
}
}
function find_student_by_lastname($lastname){
global $con;
$safe_lastname = prep($lastname);
$sql = "SELECT * ";
$sql .= "FROM studeprofile ";
$sql .= "WHERE LastName = '{$safe_lastname}' ";
$sql .= "LIMIT 1";
$student_set = mysqli_query($con, $sql);
confirm_query($student_set);
if($student = mysqli_fetch_assoc($student_set)){
return $student;
} else {
return null;
}
}
this is my current function to check if data is already exist.
function match_fistname_lastname($lastname, $firstname){
$student_firstname = find_student_by_firstname($lastname);
if($student_firstname){
find_student_by_lastname($lastname);
} else {
return false;
}
}
If you mean by "data is already exist" that a person is in the database that matches to firstname and lastname, you don't have to execute two queries.
Use the and in mysql like this:
function find_student($firstname, $lastname){
global $con;
$safe_firstname = prep($firstname);
$safe_lastname = prep($lastname);
$sql = "SELECT * ";
$sql .= "FROM studeprofile ";
$sql .= "WHERE FirstName = '{$safe_firstname}' and LastName = '{$safe_lastname}' ";
$sql .= "LIMIT 1";
$student_set = mysqli_query($con, $sql);
confirm_query($student_set);
if($student = mysqli_fetch_assoc($student_set)){
return $student;
} else {
return null;
}
}
I have many nested ifs and conditions in my code but it is not giving the desired output. What is the best way to write the following code :
$driver_code = $this->input->post('filter_driver_code');
$unit_code = $this->input->post('filter_unit_code');
$fuel_type = $this->input->post('filter_fuel');
$date_to = $this->input->post('date_to');
$date_from = $this->input->post('date_from');
if (isset($date_from) and isset($date_to) and empty($unit_code) and empty($driver_code) and empty($fuel_type)) {
$sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
$result = $this->db->query($sql);
} elseif (isset($driver_code) and isset($unit_code) and isset($fuel_type) and isset($date_from) and isset($date_to)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
$result = $this->db->query($sql);
} elseif (empty ($date_from) and empty ($date_to) and isset ($unit_code) and isset ($driver_code) and isset ($fuel_type)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
$result = $this->db->query($sql);
} else {
$sql="SELECT * FROM FUEL_USAGE";
$result = $this->db->query($sql);
}
It does not give the right output.
Try all check with empty() may be isset() gives you problem cause on post isset for all variable will return true even values blank
if (!empty($driver_code) && !empty($unit_code) && !empty($fuel_type) && !empty($date_from) && !empty($date_to)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
$result = $this->db->query($sql);
}elseif (!empty($date_from) && !empty($date_to) && empty($unit_code) && empty($driver_code) && empty($fuel_type)) {
$sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
$result = $this->db->query($sql);
} elseif (empty($date_from) && empty($date_to) && !empty($unit_code) && !empty($driver_code) && !empty($fuel_type)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
$result = $this->db->query($sql);
} else {
$sql="SELECT * FROM FUEL_USAGE";
$result = $this->db->query($sql);
}
or try in short way
$sql = "SELECT * FROM fuel_usage where 1 ";
if (!empty($driver_code)) {
$sql .= " AND driver_code='$driver_code' ";
}
if (!empty($unit_code)) {
$sql .= " AND unit_code='$unit_code' ";
}
if (!empty($fuel_type)) {
$sql .= " AND fuel_type='$fuel_type' ";
}
if (!empty($date_from) && !empty($date_to)) {
$sql .= " AND date between '$date_from' and '$date_to' ";
}
$result = $this->db->query($sql);
if (!empty($driver_code) && !empty($unit_code) && !empty($fuel_type) && !empty($date_from) && !empty($date_to)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
$result = $this->db->query($sql);
}
elseif(!empty ($unit_code) && !empty ($driver_code) && !empty ($fuel_type)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
$result = $this->db->query($sql);
}
elseif (!empty($date_from) and !empty($date_to)) {
$sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
$result = $this->db->query($sql);
} else {
$sql="SELECT * FROM FUEL_USAGE";
$result = $this->db->query($sql);
}
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";
$id=$_POST["id"];
$sqls =$handle->prepare("SELECT * FROM r WHERE ur= :u AND id='$id'");
$sqls->bindParam(':u',$_COOKIE['u']);
$sqls->execute();
$row = $sqls -> fetch();
if(!($row)){
if (!isset($_POST['submit']) && $_POST['rating'] <= 5 ) {
$sql_1 = "INSERT INTO review (b,u,r,r) VALUES(:b,:u,:r,:r)";
$query = $handle->prepare($sql_1);
$params = array(':b'=> $_POST['b'],':user'=> $_POST['u'],':r'=> $_POST[''],':ra'=> $_POST['ra']);
$query -> execute($params);
echo success;
} else {
echo nope;
}
?>
for some reason this doenst work i cant find any error is does not give any eror but just doenst do what i want
i dont want it to add an record if there has already been one in the database
Take a look in your SQL: "SELECT * FROM r WHERE ur= :u AND id='$id'"
Should "SELECT * FROM r WHERE ur= ':u' AND id=$id"
$id=intval($_POST["id"]);
$sqls =$handle->prepare("SELECT * FROM r WHERE ur= ':u' AND id=$id");
$sqls->bindParam(':u',$_COOKIE['u']);
$sqls->execute();
$row = $sqls -> fetch();
if(!($row)){
if (!isset($_POST['submit']) && $_POST['rating'] <= 5 ) {
$sql_1 = "INSERT INTO review (b,u,r,r) VALUES(':b',':u',':r',':r')";
$query = $handle->prepare($sql_1);
$params = array(':b'=> $_POST['b'],':user'=> $_POST['u'],':r'=> $_POST[''],':ra'=> $_POST['ra']);
$query -> execute($params);
echo success;
} else {
echo nope;
}
?>