I'm newbie in PHP sorry. How can i update a user defined var in user defined table with user defined value in MySql? I cant get it work.
<?PHP
$table = $_POST['table'];
$id = $_POST['id'];
$key = $_POST['key'];
$value = $_POST['value'];
$con = mysql_connect("mysql.serversfree.com","u105645000***","mf***") or ("Cannot connect!" . mysql_error());
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("u10564500***" , $con) or die ("could not load the database" . mysql_error());
/*$mysql = mysql_query("INSERT INTO $table ($key) VALUES ('".$value."');");*/
$sql = "UPDATE $table Set ".$key."='".$value."'";
?>
Why your code is not working
You're not launching the query.
No where clause
Without specifying any conditions, every record of the coulmn $key will be updated with $value.
Solution
Use this code:
<?PHP
$con = mysqli_connect("mysql.serversfree.com","u105645000***","mf***","u10564500***");
$table = mysqli_real_escape_string($con,$_POST['table']);
$id = mysqli_real_escape_string($con,$_POST['id']);
$key = mysqli_real_escape_string($con,$_POST['key']);
$value = mysqli_real_escape_string($con,$_POST['value']);
if (!$con){die('Could not connect: ' . mysqli_error($con));}
$sql = mysqli_query($con,"UPDATE ".$table." Set ".$key."='".$value."'");
?>
Related
When trying to post data to the database it gives an error saying no database selected. But the content from the database is displaying on a table and select menu.
<?php
$con = mysqli_connect("localhost", "root", "","radian");
if(!$con)
{
exit("Couldn't connect: ".mysqli_connect_error());
}
mysqli_set_charset($con, "utf8");
$insert_data = "UPDATE enquiries
SET ResponseDate = '".$current_date."',
Response = '".$txtResponse."',
Enquiry_No = '".$_SESSION['ses_staff']
."' WHERE Enquiry_No = '".$txtStudentId."'";
$execute = mysql_query($insert_data) or die(mysql_error());
$output= '<h4 style="margin-left:1em;width:15em;color:red;"> Response successful!. </h4>';
}else{
$output= '<h4 style="margin-left:1em;width:15em;color:red;"> </h4>';
}
Your final code should be identical to:
<?php
$con = mysqli_connect("localhost", "root", "", "radian");
if (!$con) {
exit("Couldn't connect: " . mysqli_connect_error());
}
mysqli_set_charset($con, "utf8");
$insert_data = "UPDATE enquiries SET ResponseDate = '" . $current_date . "', Response = '" . $txtResponse . "',Enquiry_No = '" . $_SESSION['ses_staff'] . "' WHERE Enquiry_No = '" . $txtStudentId . "'";
$execute = mysqli_query($con, $insert_data) or die(mysqli_error($con));
$output = '<h4 style="margin-left:1em;
width:15em;
color:red;"> Response successful!. </h4>';
Changes made:
Remove all instances of mysql_* and replace with the correct mysqli_* function.
Remove the orphaned } else {.
Note: Officially mysql_* functions are deprecated. So no point using them. Use either mysqli_* or PDO.
I am trying to import xml data into a mysql table. I have the below fields to be Imported:
reference
price
category
type
city
property
imgurl1
imgurl2
The problem is that the number of <imgurl>(url to image file) is not the same. Below is the code:
$conn = mysql_connect($hostname, $username, $password);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname,$conn) or die( mysql_error() );
$xml = simplexml_load_file('http://astonpearlemail.net/feeds/feedsmall.xml');
$data = $fields = array();
foreach ($xml->xpath('listing') as $listing) {
$fields = array_keys((array)($listing));
$data[] = '("' . join('", "', (array)$listing) . '")';
}
$sql = "INSERT INTO ap_prop (" . join(', ', $fields) . ") VALUES\n" ;
$sql .= join (",\n", $data);
$result1 = mysql_query($sql,$conn);
echo "<pre>$sql</pre>"
Please suggest how I can import the varying number of imgurl.
Thanks in advance
I have a script that reads my database table fields. Its not reading the first column which is the id.It reads the other fields and adds them into the array. I have added in the for loop a -1 to get every field but to no success.
$host=rtrim($_POST['host']);
$user=rtrim($_POST['user']);
$pass=rtrim($_POST['pass']);
$dbselect=rtrim($_POST['dbselect']);
$table=rtrim($_POST['table']);
$classname=rtrim($_POST['classname']);
$key_values = array();
$link = mysql_connect($host,$user,$pass);
$db_select = mysql_select_db($dbselect);
$query = mysql_query('SHOW COLUMNS FROM '.$table.'');
if (!$link) {
die('Could not connect to MySQL server: ' . mysql_error());
}
$dbname = $dbselect;
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
die("Could not set $dbname: " . mysql_error());
}
$res = mysql_query('select * from '.$table.'', $link);
$num_fields = mysql_num_fields($res);
for($i=0;$i<$num_fields;$i++){
$key_values[]=mysql_field_name($res,$i);
}
echo "<pre>";
print_r($key_values);
echo "</pre>";
There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.
<?php
$host=rtrim($_POST['host']);
$user=rtrim($_POST['user']);
$pass=rtrim($_POST['pass']);
$dbselect=rtrim($_POST['dbselect']);
$table=rtrim($_POST['table']);
$classname=rtrim($_POST['classname']);
$db = new mysqli($host,$user,$pass,$dbselect);
if($db->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
// NOTE real_escape_string may not work for tables untested
$result = $db->query("SELECT * FROM " . $db->real_escape_string($table));
if (!$result)
die "Error: " . $db->error;
while ($row = $result->fetch_object())
{
echo $row->id;
}
$result->close();
$db->close();
I don't see why it might be doing that, but this should be more reliable:
$query = mysql_query('select * from `%s`', mysql_real_escape_string($table), $link);
while ($result = mysql_fetch_array($query)) {
print_r(array_keys($result));
}
Try to use php native function mysql_fetch_array (also you need view this quastion before)
After try this code ($res === 'resources'):
$res = mysql_query('select * from '.$table.'', $link);
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
$key_values[] = array_keys($row);
}
echo "<pre>";
print_r($key_values);
echo "</pre>";
I've got this:
foreach($_POST['pos'] as $value) {
$new_value = "UPDATE users SET regnr='" . $value . "'
WHERE username='" . mysql_real_escape_string($_COOKIE['username']) . "'";
}
// Connect to database
$opendb = mysql_connect($dbhost, $dbuser, $dbpass) or die("Kunde inte ansluta till MySQL:<br>" . mysql_error());
mysql_select_db($dbname) or die("Kunde inte ansluta till databasen:<br>" . mysql_error());
mysql_query($new_value) or die(mysql_error());
// Close database
mysql_close($opendb);
Information:
$_POST['pos'] holds a value from the database in a hidden input. This value I have choosen to split with str_split($r['regnr'], 6); into a JQuery sortable list. If I type echo $value; in the foreach loop I've got the new value (not splitted, as I want) from the JQuery sortable list. I need all values from the list, and I get it with echo. But if I use $value variable to UPDATE the database that it came from, it just updates with the last value from the JQuery sortable list.
Can someone solve that? :D
Here is the solution:
$str = '';
foreach($_POST['pos'] as $value) {
$str = $str.$value;
}
// Connect to database
$opendb = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$new_value = "UPDATE users SET regnr='" . $str . "' WHERE username='" . mysql_real_escape_string($_COOKIE['username']) . "'";
mysql_query($new_value) or die(mysql_error());
// Close database
mysql_close($opendb);
I'm using the following code in PHP.
Its creating the file post.json, but inserting values into the table is not reflecting.
<?php
$input = file_get_contents('php://input');
logToFile("post.json",$input);
$json_a = json_decode($input,true);
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
mysql_query("INSERT INTO order_table (Table_id, Menu_Item_Name, Menu_Item_Price, Menu_Item_quantity) VALUES('$json_a[TableId]','$json_a[ItemName]','$json_a[ItemPrice]','$json_a[ItemQuantity]')");
function logToFile($filename,$msg)
{
$fd = fopen($filename,"a");
$str="[".date("Y/m/d h:i:s")."]".$msg;
fwrite($fd,$str."\n");
fclose($fd);
}
?>
Try to replace '$json_a[foo]' with '" . $json_a['foo'] . "' or '${json_a['foo']}'.