How to update many fields with single query? - php

I am working with a MLM company where com has to register the member, if it detects any mistake in data, it has to update the data of member again.
I have the right code which works well when all fields has to update but problem is it does not work for individual input boxes. Please give me solution of this problem.
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'vvvv';
$dbpass = 'xxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$usrid = $_POST['uid'];
$pwrd = $_POST['pwd'];
$nm = $_POST['noe'];
$fnm = $_POST['fn'];
$addrs = $_POST['adrs'];
$cntn = $_POST['cnt_no'];
$cty = $_POST['ct'];
$sql = "UPDATE office_user ".
"SET
password = '$pwrd',
name='$nm',
father_name='$fnm',
address='$addrs',
contact_no='$cntn',
city='$cty' ".
"WHERE user_id = '$usrid'" ;
mysql_select_db('my_db');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>

Related

Mysql error "Access denied for user"

I am trying to backup mysql database using php.
I tried the code below:
<?php
$dbhost = 'my_host';
$dbuser = 'my_user';
$dbpass = 'my_pass';
$db = 'my_dbname';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $db);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$table_name = "my_table";
$backup_file = "/tmp/my_table.sql";
$sql = "SELECT * INTO OUTFILE '$backup_file' FROM $table_name";
mysql_select_db($db);
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not take data backup: ' . mysql_error());
}
echo "Backedup data successfully\n";
mysql_close($conn);
?>
but I got this error:
"Could not take data backup: Access denied for user 'my_user'#'%' (using password: YES)"
Could you help me, please? Thanks!
Try
<?php
$dbhost = 'my_host';
$dbuser = 'my_user';
$dbpass = 'my_pass';
$db = 'my_dbname';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $db);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$table_name = "my_table";
$backup_file = "/tmp/my_table.sql";
$sql = "SELECT * INTO OUTFILE '$backup_file' FROM $table_name";
// set current db with connection resource
mysql_select_db(db, $conn);
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not take data backup: ' . mysql_error());
}
echo "Backedup data successfully\n";
mysql_close($conn);

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'");

php mysql data rerival from table

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = "";
$name=$_POST['user'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM `usertable` WHERE `username`='$name'' ;
mysql_select_db('myxiv');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Name :{$row['username']} .<br> ";
}
mysql_close($conn);
?>
Here is my code and i'm getting this error.
Syntax error, unexpected '$name' (T_VARIABLE)
I m trying to retrive data from a table to display in the profile page
$sql = 'SELECT * FROM usertable WHERE username='$name'' ;
Replace Your above query by bellow code and check it.
$sql = "SELECT * FROM usertable WHERE username=' ".$name." ' ";
Try this
Change
$sql = 'SELECT * FROM usertable WHERE username`='$name'' ;
TO
$sql = "SELECT * FROM usertable WHERE username='".$name."';
You make a few mistakes. mysql (which is depracted (take a look at PDO or mysqli)) doesn't need a connection.
You could have a double single quote error but the following should fix some of it:
if($_POST) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$name = mysql_real_escape_string(strip_tags($_POST['user']));
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('myxiv');
$retval = mysql_query("SELECT * FROM `usertable` WHERE `username` = '$name'");
if(! $retval ) {
die('Could not get data: ' . mysql_error());
} else {
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Name :{$row['username']} .<br> ";
}
}
}

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" ;

Categories