PHP header not working - php

I have a web page that will get the data using POST method from the other page and then store the data to the database. After it's done, I want it to go back to the page where the data comes from. I used header(). But it didn't work.
Here's my code:
<?php
$date = $_POST['date'];
$spent = $_POST['spent'];
$description = $_POST['description'];
$dbc = mysqli_connect('oniddb.cws.oregonstate.edu', 'chentao-db', 'BUU3UuwwGfwt7hxV', 'chentao-db')
or die(mysql_error());
$query = "SELECT Total FROM Expense E ORDER BY E.Index DESC LIMIT 1";
//get the total
$result = mysqli_query($dbc, $query) or die(mysql_error());
$total = mysqli_fetch_array($result, MYSQL_ASSOC);
$total = $total['Total'] + $spent;
$query = "INSERT INTO Expense (Date, Spent, Total, Description) VALUES ('$date', '$spent', '$total', '$description')";
$result = mysqli_query($dbc, $query) or die(mysql_error());
mysql_close($dbc);
header("Location: fetchData.php");
?>
What's wrong with it? Thank you.

I guess it would work something like this
header("Location: http://".$_SERVER['HTTP_HOST']."/fetchData.php");

try to place
ob_start();
at top of your file..
Or you can try
header('Location: '.$_SERVER['HTTP_REFRER']);
die;
don't forget to place die at last.

If your first query does not return any result, then the execution fails at this line:
$total = $total['Total'] + $spent;
Check if the first query has any results like this:
if(isset($total['Total'])){
$total = $total['Total'] + $spent;
}
else {
$total = $spent;
}

Related

PHP/MySQL: Passing values from one query to another in PHP multi queries

I have to execute 3 queries inside one PHP file. I have used mysqli_multi_query(). The first 2 queries are SELECT queries which return values for third query (INSERT). So far I have done this;
<?php
$host = "localhost";
$user = "smartbusarrival_grouplog";
$password = "group10#10";
$db = "smartbusarrival_sbaDB";
$u_id = 51;
$b_id = 1;
$t_id = 1;
$date = '2017-06-30';
$startHalt = "Kaduwela";
$endHalt = "Nugegoda";
$seats = array(42,43);
$con = mysqli_connect($host,$user,$password,$db);
$query = "CALL getHaltTag('$t_id','$startHalt');";
$query .= "CALL getHaltTag('$t_id','$endHalt');";
$query .= "INSERT INTO reservation (user_id,bus_id,trip_id,date,start,end) values ('$u_id','$b_id','$t_id','$date','$startTag','$endTag')";
if(mysqli_multi_query($con, $query)){
do{
if($result = mysqli_store_result($con)){
while ($row = mysqli_fetch_array($result)){
$startTag = $row[0];
$endTag = $row[1];
}
mysqli_free_result($result);
}
}
while (mysqli_next_result($con));
}
mysqli_close($con);
?>
First two queries work very well and give correct answers.
Code works just fine when inserting a new record. But the value of start and end are zero.
You can't use multi-query for this, because you're substituting the variables into the third query before you perform the fetch that sets them. Just do them as separate queries.
$query = "CALL getHaltTag('$t_id','$startHalt');";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_row($result);
$startTag = $row[0];
$query = "CALL getHaltTag('$t_id','$endHalt');";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_row($result);
$endTag = $row[0];
$query = "INSERT INTO reservation (user_id,bus_id,trip_id,date,start,end) values ('$u_id','$b_id','$t_id','$date','$startTag','$endTag')";
mysqli_query($con, $query);
In general, there are very few situations where multi-query is really needed, and it usually complicates the code.
And if you change getHaltTag from a stored procedure to a stored function, you don't need 3 separate queries. You could do it in a single query where you call the function:
$query = "INSERT INTO reservation (user_id,bus_id,trip_id,date,start,end)
values ('$u_id','$b_id','$t_id','$date',getHaltTag('$startHalt'),getHaltTag('$endHalt'))";

How to get total number of row by using this function php mysql?

How to get total number of row by using this function php mysql ?
i use this code for display data from mysql. It's work good,
Buy i want to know can i get total number of row by using this code ?
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
Try this mysql_num_rows ($result)
Use this line of code
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
$number_of_rows = mysql_num_rows($result); // this will return the number of rows found by the $result query.
echo $number_of_rows;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>

How to have 2 insert queries on a single php file

I want to insert data from 2 different tables in a single php file. Only the first insert query will be executed. please help, thank you!
<?php
//session_start();
include('config.php');
//$_SESSION['ID'] = $ID
//Data from guestAdd.php
$aID = $_POST['id'];
$afrom = $_POST['from'];
$ato = $_POST['to'];
$aleaveType = $_POST['leaveType'];
$selectRemarkSql = mysqli_query($conn, "SELECT remarkName FROM remarks WHERE remarkID = '$aleaveType';");
while($row = $selectRemarkSql ->fetch_assoc()) {
$leaveType = $row["remarkName"];
}
$addAbsentSql = mysqli_query($conn, "INSERT INTO absent(afrom, ato, aleaveType, empID) VALUES('$afrom', '$ato', '$leaveType', '$aID');");
$leaveLogUpdateSql = mysqli_query($conn, "INSERT INTO attendance(empID, HoursWorked, remarks, holiday) VALUES('$aID', 00:00:00, '$aleaveType', 'None');")
header("Refresh: viewAllEmployees.php");
?>
Check for errors between the queries. Make sure to stop the header refresh so that the error message is displayed.
$addAbsentSql = mysqli_query($conn, "INSERT INTO absent(afrom, ato, aleaveType, empID) VALUES('$afrom', '$ato', '$leaveType', '$aID');");
echo myslqi_error($conn);
$leaveLogUpdateSql = mysqli_query($conn, "INSERT INTO attendance(empID, HoursWorked, remarks, holiday) VALUES('$aID', 00:00:00, '$aleaveType', 'None');")
//header("Refresh: viewAllEmployees.php");

Is it possible to concatenate this INSERT query in and out of a if statment?

I'm having a few issues with this silly query, was wondering is it possible to concatenate the following INSERT query inside the IF statement and outside it as well to complete the rest of the query. So I want the $orderid to be insert within the if statement and the rest of the last 3 variables outside the IF
if(isset($_POST['Submit'])){
$orderid=mysql_insert_id();
$sql = mysql_query("SELECT * FROM course WHERE serial='$serial'") or die(mysql_error());
$fetch = mysql_fetch_assoc($sql);
$serial = $fetch['serial'];
$price = $fetch['price'];
mysql_query("INSERT into course_order_detail values ('$orderid','$serial','1','$price')") or die(mysql_error());
}
Oh and $orderid is from the previous insert query written in my code.
Try Like this
if(isset($_POST['Submit'])){
$orderid=mysql_insert_id();
$sql = mysql_query("SELECT * FROM course WHERE serial='$serial'") or die(mysql_error());
$fetch = mysql_fetch_assoc($sql);
$serial = $fetch['serial'];
$price = $fetch['price'];
$in = mysql_query("INSERT into course_order_detail values ('$orderid')") or die(mysql_error());
$new_id = mysql_insert_id();
}
$up = mysql_query("UPDATE course_order_detail SET serial='$serial',quantity='1',price='$price' WHERE orderid = ".$new_id);
You can use a nested insert with select
INSERT `into course_order_detail` SELECT '$orderid', serial, '1', price FROM course WHERE serial='$serial'
BTW sanitize your queries

PHP not displaying data

Any ideas why this simple php code won't display results when trying to echo the data.
<?php
{
mysql_connect("localhost" , "" , "") or die (mysql_error());
mysql_select_db("") or die(mysql_error());
$pid=intval($_SESSION["User_id"]);
$query = "SELECT `car`, `details`, `price` FROM `Car``";
//executes query on the database
$result = mysql_query ($query) or die ("didn't query");
//this selects the results as rows
$num = mysql_num_rows ($result);
while($row=mysql_fetch_assoc($result))
{
$_SESSION['car'] = $row['car'];
$_SESSION['details'] = $row['details'];
$_SESSION['price'] = $row['price'];
}
}
?>
<?php echo $_SESSION['car']; ?>
<?php echo $_SESSION['details']; ?>
<?php echo $_SESSION['price']; ?>
Just testing at the moment to see if the car, price and details display from the database and they don't seem to.
You missed session_start(); at start of page and change
$query = "SELECT `car`, `details`, `price` FROM `Car``";
^
to
$query = "SELECT `car`, `details`, `price` FROM `Car`";
Are you expecting one or many results for this query ?
If many results, you are saving the last entry in the session.
If only one, just do : $row=mysql_fetch_assoc($result) instead of this while.
Check the query.Try to echo it, copy, paste in MySQL and run it. But you have $pid, have you put it in the query?
$query = "SELECT car, details, price FROM Car WHERE id = $pid ";
I rather remove all backticks since non of those identifiers are reserved keywords.
$query = "SELECT car, details, price FROM Car";

Categories