Update mySQL query didn't work - php

I have been working on some student management system where student login and register courses, login machanism works fine after login it display student name, id etc and ask to register courses, when student select courses, the selected courses should be updated to database, that submit page need to get student id and courses from previous page and update the database accordingly.
After submitting it only update courses as $fresh :S
Here's submit.php code:
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = '123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$fresh = $_REQUEST["fresh"];
$user_id= $_REQUEST["user_id"];
$sql = 'UPDATE courses
SET fresh="$fresh"
WHERE user_id=$user_id';
mysql_select_db('registrations');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
?>

try this code of block.
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = '123';
$database='registrations';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$database);
if (!$conn || $conn==null) die('Could not connect: ' . mysqli_error($conn));
$fresh = $_REQUEST["fresh"];
$user_id= $_REQUEST["user_id"];
$sql = "UPDATE courses
SET fresh='$fresh'
WHERE user_id=$user_id";
$retval = mysqli_query($conn, $sql);
if(! $retval )
{
die('Could not update data: ' . mysqli_error($conn));
}
else{
echo "Updated data successfully\n";
}
mysqli_close($conn);
?>

Here is the idea:
<?php
$fresh = "test_string";
$user_id = 1;
$sql = 'UPDATE courses SET fresh="$fresh" WHERE user_id=$user_id';
$corrected_sql = "UPDATE courses SET fresh='$fresh' WHERE user_id=$user_id";
echo '<pre>';
echo "\$sql output: $sql" . "\n";
echo "\$corrected_sql output: $corrected_sql";
echo '</pre>';
?>
The output is like this:
$sql output: UPDATE courses SET fresh="$fresh" WHERE user_id=$user_id
$corrected_sql output: UPDATE courses SET fresh='test_string' WHERE user_id=1
You can notice how the difference between single and double quotes. Hope it helps!

Variables under single quotes will never be parsed.
Your actual query was like this..
$sql = 'UPDATE courses SET fresh="$fresh" WHERE user_id=$user_id';
^----- This one This one ---------^
So rewrite it like
$sql = "UPDATE `courses`
SET `fresh`='$fresh'
WHERE `user_id`=$user_id";

you need to put the php variables in quote
try this sql
$sql = "UPDATE courses
SET fresh='$fresh'
WHERE user_id='$user_id'";

Remove double quotes of $fresh and try with this.
$sql = "UPDATE courses
SET fresh='$fresh'
WHERE user_id=$user_id";

'UPDATE courses
SET fresh="$fresh"
WHERE user_id=$user_id';
replace it with
"UPDATE `courses` SET `fresh`='$fresh' WHERE `user_id`='$user_id' ";

Related

Query failed in php

$query = "SELECT * FROM Student WHERE student_id = $child AND school_id = $college";
$result = mysqli_query($conic, $query);
if(!$result){
die(" query failed");
echo 'error here';
} else {
i am trying to retrieve some information from the SQL database but the query seems to fail any ideas?
You must learn more : how to retrieve data from database using select in php
so try out following example to display all the records from Student table
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM student WHERE student_id = '"$child"' AND school_id = '"$college"' ";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
// some code here
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
NOTE
The mysql extension is deprecated and will be removed in the future:
use mysqli or PDO

Query was empty error | Trying to get a specific Mysql Value

Thanks in advance for any help you could bring, I'm really noob on all coding stuffs but this is the situation:
I have a wordpress website focused on Travel Agency Tours and hotels, and I'm working on an extranet system, the idea is that hotel managers will be able to change the price of their rooms, but this extranet works totally apart of wordpress, its a php script, with a different mysql database, so, what I would like to do is that when the price is changed in the extranet, then in wordpress this is going to be also changed in real time, so I'm trying in this way:
I'm planning to insert a php snippet on wordpress editor with a mysql code to get the price from the other database for each hotel and room, so, I'm going to do this for every hotel manually, the problem becomes from the code im trying to use to get that value:
<?php
$dbhost = 'localhost';
$dbuser = 'test_user';
$dbpass = 'pass';
$dbname = 'test_db';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = mysql_query("SELECT `room_price`, FROM `hotel_room_price` WHERE price_id = '1'");
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Room Price :{$row['room_price']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
So the result I get in my test.php file with this code is:
"Could not get data: Query was empty"
I'm not sure why is this error, and why is not printing the value I want.
PostData: I really appologize for my english since is not my language, but I'm trying to learn.
and try connection like this
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);
You need to remove coma from your query after room price
//You are running the query twice so use this query
$sql = "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'";
and learn mysqli or PDO as mysql are deprciated and soon going be drop
you have send the query two times...
try this
$sql = "SELECT `room_price`, FROM `hotel_room_price` WHERE price_id = '1'";
$retval = mysql_query( $sql, $conn );
<?php
$dbhost = 'localhost';
$dbuser = 'test_user';
$dbpass = 'pass';
$dbname = 'test_db';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$db_selected = mysql_select_db($dbname,$conn);
if (!$db_selected) {
die ('Can\'t use test_db : ' . mysql_error());
}
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Room Price :{$row['room_price']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
For select database use mysql_select_db() . you can't pass dbname as an args in mysql_connect()
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);
Also if you are learner then learn mysqli or PDO
Note :- mysql_* has been deprecated
you are calling query run two times so change below lines
$sql = mysql_query("SELECT `room_price`, FROM `hotel_room_price` WHERE price_id = '1'");
$retval = mysql_query( $sql, $conn );
to
$retval = mysql_query("SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'");

syntax error when delete sql data using php

im getting the following syntax error can someone please help!
im guessing it something soooo easy but i have been looking at it for ages and can see what im doing wrong
<?php
if(isset($_POST['delete']))
{
$dbhost = '';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$CourseId = $_POST['CourseId'];
$sql = "DELETE course ".
" WHERE CourseId = $CourseId" ;
mysql_select_db('d11os_projectdb');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
}
else
{
?>`enter code here`
$sql = "DELETE FROM course ". --<-- Missing key word FROM
" WHERE CourseId = $CourseId"
You are missing the table name from the sql query
$sql = "DELETE course FROM **table_name**".
" WHERE CourseId = $CourseId" ;

mysql update query is not running

MySQL update query is not working on the php based webpage also not showing any MySQL error but when copy the query and run it in SQL it works fine.
Here is my code:
$query = "UPDATE table_name SET page_name = '".$page_name."' WHERE ip = '".$ip."'";
$update = mysql_query($query) or die(mysql_error());
please if anyone sees anything wrong in the code please let me know.
You should try this syntax instead:
$query = "UPDATE table_name SET page_name = '$page_name' WHERE ip = '$ip'";
$update = mysql_query($query) or die (mysql_error());
Please note that mysql_* methods are deprecated and you should use mysqli_* methods instead.
Demo Code : Its Work Fine for me.Please Use it.
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$page = 'Hello';
$ip = '102.101.22.23';
$sql = "UPDATE `pages`
SET `pagename` = '".$page."'
WHERE `ip` = '".$ip."'";
/* OR use $sql = "UPDATE pages SET pagename= '".$page."' WHERE ip='".$ip."'"; */
mysql_select_db('demo');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);

delete from sql with a delete.php file

i am wanting to build a delete page to use to delete some data from my database.
when i put the id in it works fine but i am wanting it to pull the id from the url
www.example.com/delete.php?id=1234
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'DELETE FROM MYTABLE
WHERE created=<?php echo $_GET["id"]; ?>';
mysql_select_db('ely');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
?>
the part im having issues with is this part
WHERE created=<?php echo $_GET["id"]; ?>';
the error i am getting is "Could not delete data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=' at line 2"
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$id = mysql_real_escape_string($_GET['id']);
$sql = "DELETE FROM MYTABLE
WHERE created='$id'";
mysql_select_db('ely');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
?>
learn quoting!
$sql = 'DELETE FROM MYTABLE
WHERE created=<?php echo $_GET["id"]; ?>';
$sql = 'Delete from MYTABLE WHERE created="'.$_GET['id'].'"';
Next thing is, google for sql-injections, and keep in mind that you should proof if $_GET['id'] is set in time
To concatenate a variable into a string, use the concatenation operator.
But your code is bad for severalreasons.
1: Sanitise your inputs
2: Don't use GET for anything that can change the state of the server, use POST instead.
Here's a story explaining one reason why
try this:
$id = $_GET['id'];
$sql = 'DELETE FROM MYTABLE
WHERE created=' .$id;
"." is used to concatenate the value of the variable $id
As for connecting to a db i suggest you to look up for PDO.
$sql="DELETE FROM MYTABLE WHERE created='".$_GET['id']."'";

Categories