Why am I getting this error "Error: Query was empty" - php

I am trying to update my SQL database using a form through php, but i keep getting the error "Error: Query was empty".
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
mysql_query($sql, $con);
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
mysql_close($con);
?>
It also won't update my table and I don't know what I've done wrong. All help will be much appreciated. I am new to this as you can probably tell!

There is an error in your code first you are calling mysql_query($sql, $con); without any query in your $sql variable your $sql is blank ""
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
mysql_close($con);
?>

Remove mysql_query($sql, $con) query execution after db selection because $sql is empty,
Also put your update sql execution in IF conditions, because if its not true than again $sql will be empty and you will get same error again,...
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
// mysql_query($sql, $con); // <-- remove this
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
}
mysql_close($con);
?>

Related

Form Not Redirecting After Submit Header Location in PHP Not Working

My PHP header redirection isn't working and I can't seem to figure out why. I've read through a lot of questions and nothing I've tried seems to work.
<?php
define('DBHOST','shareddb1d.hosting.stackcp.net');
define('DBUSER','JoplinLeftHand-3231135a');
define('DBPASS','Banoodle1!');
define('DBNAME','JoplinLeftHand-3231135a');
$link = mysql_connect(DBHOST, DBUSER, DBPASS);
if (!link) {
die('Could Not Connect: ' . mysql_error());
} else {
echo "You Are Connected<br>";
}
$db_selected = mysql_select_db(DBNAME, $link);
if (!$db_selected) {
die('Can\'t Use ' . DBNAME . ': ' . mysql_error());
} else {
echo "Database Selected";
}
$tid = $_POST['tid'];
$first = $_POST['first'];
$last = $_POST['last'];
$zip = $_POST['zip'];
$descrip = $_POST['descrip'];
$sql = "INSERT INTO tracking (tracking_id, tracking_first, tracking_last, tracking_zip, tracking_descrip) VALUES ('$tid', '$first', '$last', '$zip', '$descrip')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
} else {
header("Location:/?p5");
}
header('Location: ...'); has to be the first output of the script, otherwise it will not work. You are using echo twice before performing the redirect, here:
echo "You Are Connected<br>";
and here:
echo "Database Selected";
Also, SOME clients require the URL passed in the Location header to be absolute URLs, and not relative, for example:
header("Location: http://example.com/?p5");

How to use Insert query in PHP code

I do not understand what is going wrong with code. The result is get is "connected successfully success Query failed". I tried few combinations and I get the same result. Please help me in solving this. Thanks in advance.
<?php
$link = mysql_connect('localhost', 'root1', '')
or die('Could not connect: ' . mysql_error());
if ($link) {
echo 'connected successfully';
}
$l = mysql_select_db('vtflix', $link) or die ('Could not select the database');
if ($l) {
echo ' success';
}
/*$varCNAME = 'John';
$varCONTENT = '4';
$varVID = '1';*/
$sql = "INSERT INTO mpaa(C_Name, ContentRating, V_ID) VALUES ('Jon', 4, 3)";
mysql_query($sql, $link) or die("Query failed");
$que = "SELECT * FROM mpaa";
$query = mysql_query($que, $link);
if (!$query) {
echo 'query failed';
}
while ($sqlrow = mysql_fetch_array($query, MYSQL_ASSOC)) {
$row = $sqlrow['C_Name'];
$nrow = $sqlrow['Content Rating'];
$mrow = $sqlrow['V_ID'];
echo "<br>" . $row . " " . $nrow . " " . $mrow . "<br>";
}
mysql_close($link);
?>
1.Don't use mysql_* library (deprecated from php5 onward + removed from php7) .Use mysqli_* OR PDO.
2.An example of mysqli_*(with your code)is given below:-
<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
Note:- To check php version (either on localhost or on live server) create a file with name phpInfo.php, and just write one line code in that file:-
<?php
phpinfo();
?>
Now run this file and you will get the current php version.
Like this:- https://eval.in/684551
Here it seems that you are using deprecated API of mysql_* .
1) Check your PHP version
<?php phpinfo();exit;//check version ?>
2) avoid the usage of mysql use mysqli or PDO
3) change your db connection string with this :
new Mysqlidb($hostname, $username, $pwd, $dbname);
example with you code
<?php
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>

How to get notification when insert new value in db?

Hi i am inserting value in data base in php i want that when i insert value in database then my div color should be change
insert.php
include('conn.php');
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$id=2;
//$sql="insert into messages (message,number,service) values ('dd',".$urlstring.",'ds')";
$sql = 'INSERT INTO messages '.
'(message,number,service) '.
'VALUES ( "'.$message.'", "'.$urlstring.'", "'.$service.'" )';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo ("Records affcted: ". mysql_affected_rows());
//echo "Entered data successfully\n";
mysql_close($conn);
index.php
<div id="div1" style="height=200px;width=300px;">
</div>
here i want when insert.php execute then div section should change color in another file
How can i achieve this
Any help will be appreciated
You can use timer on javascript and create a ajax function to retrieve all inserted data on the database.
try this:
include('conn.php');
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$id=2;
//$sql="insert into messages (message,number,service) values ('dd',".$urlstring.",'ds')";
$sql = 'INSERT INTO messages '.
'(message,number,service) '.
'VALUES ( "'.$message.'", "'.$urlstring.'", "'.$service.'" )';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo ("Records affcted: ". mysql_affected_rows());
//echo "Entered data successfully\n";
?>
<style>
#div1{
backgroud-color="yourcolor";
}
</style>
<?php
mysql_close($conn);
ah i see.
maybe this can help you: http://www.barelyfitz.com/projects/csscolor/?

php mysql query string

I need help to query string from database please help.
<?php
$phone="8165526693#vtext.com";
$link = mysql_connect('localhost', 'root', 'toor');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('wizarddb')) {
die('Could not select database: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM Phone WHERE phone LIKE '%$phone%'");
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result(1); // outputs phone
mysql_close($link);
?>
I have issues with echo or is the query wrong?
Your error is on the echo.
echo mysql_result($result(1); // outputs phone
That SHOULD be
echo mysql_result($result[1]); // outputs phone

Strange behaviour trying to detect errors selecting a database

I have the following code in php:
<?php
$con = mysql_connect("localhost","john","john");
mysql_select_db("data", $con);
if (!$mysql_select_db) {
die ('Cannot use data : ' . mysql_error());
}
$result = mysql_query("SELECT * FROM test where huss='hussein'");
while ($row = mysql_fetch_array($result)) {
echo "ADSDAD";
echo "<br />";
}
if (!$con) {
die('ERROR Could not connect: ' . mysql_error());
}
mysql_close($con);
?>
I was able to connect to the MySQL server, but I can't seem to select the relevant database.
Being printed is the hard-coded text Cannot use data :, but no MySQL error message follows it. If the MySQL connection had failed then I would have expected a MySQL error message to appear after the hard-coded text.
What am I doing wrong?
The problem's here:
mysql_select_db("data", $con);
if (!$mysql_select_db) {
die ('Cannot use data : ' . mysql_error());
}
You don't have a variable $mysql_select_db, and it doesn't magically map to the result of the last time you called mysql_select_db.
So, instead:
$result = mysql_select_db("data", $con);
if (!$result) {
die ('Cannot use data : ' . mysql_error());
}
Or:
if (!mysql_select_db("data", $con)) {
die ('Cannot use data : ' . mysql_error());
}

Categories