Insert the values from the array into the column from the array? - mysql

I am trying to run the following but am getting the following mysql error ?
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 'INSERT INTO hqfjt_chronoforms_data_addupdatelead SET f9f2ec4270a751f4f34980c325e' at line 2
<?php
$user = $_POST[cf_uid];
$form = $_POST[uid];
$date = date("d-m-Y");
$query = mysql_query("UPDATE hqfjt_chronoforms_data_addupdatelead SET $form = $date
WHERE cf_uid = $user
")
or die(mysql_error());
?>
what I am trying to do, is use the $USER to find the correct user record, then in that user record find the column $form and insert the $date into it,
EDIT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Ok this gets me halfway there, but still an error >>
<?php
$user = $_POST[cf_id];
$form = $_POST[uid];
$date = date("d-m-Y");
$query = mysql_query("UPDATE hqfjt_chronoforms_data_addupdatelead SET '".$form."' = '".$date."' WHERE cf_id = '".$user."'")
or die(mysql_error());
?>
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 ''185cfb5654aacf3038e3f26491f227356b5d768f' = '30-12-2011' WHERE cf_id = '14'' at line 1

This is not correct:
First you are trying to execute both select and insert together and second insert don't have set command ... I think you need an update command
update hqfjt_chronoforms_data_addupdatelead SET $form = $date
WHERE $user = $user
OR, I think you are trying to do something like this
INSERT INTO hqfjt_chronoforms_data_addupdatelead SELECT * FROM
hqfjt_chronoforms_data_addupdatelead WHERE $user = $user
EDIT:
Try This:
<?php $user = $_POST["cf_uid"];
$form = $_POST["uid"];
$date = date("d-m-Y");
mysql_query('UPDATE hqfjt_chronoforms_data_addupdatelead SET "$form" = "$date"
WHERE cf_uid = "$user"') or die(mysql_error()); ?>

as per your comment you'd be fine just doing a
$query = "UPDATE table SET $forms = $date WHERE cf_uid = $user";
$success = mysql_query($query);
or you can put it all into one line as well. But if you're just looking to update there's no need to SELECT ALL the data from the user. That's what the "WHERE" is for.

Related

Can't get a simple SELECT to work

I have done this type of SELECT many times, but this time I can't get it to work. Any ideas, please?
$Name = "Dick";
$conn = mysqli_connect($server, $dbname, $dbpw, $dbuser);
$sql = "SELECT id FROM table WHERE $Name = table.first_name";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$customer_id = $row['id'];
Database::disconnect();
echo "customer id = " . $customer_id;
If you really DO have a table named table it would be more appropriate to use back ticks around the name since the word TABLE is a reserved word in MySQL. You should also use single quotes around your variable if it contains a string:
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
Other possible reasons if the query still doesn't work for you:
Make sure you have the connection parameters in the right order. It should be: mysqli_connect($server, $dbuser, $dbpw, $dbname).
You should be using fetch_array() instead of fetch_assoc() if you expect a one row result.
You are mixing PROCEDURAL STYLE with Object Oriented Style when using mysqli_connect() instead of mysqli(), at the same time using $result-> which is object oriented style. You should decide one style and stick with it.
This would be the procedural style of your query:
$Name = "Dick";
$conn = mysqli_connect($server, $dbuser, $dbpw, $dbname); // NOTE THE CHANGED ORDER OF CONNECTION PARAMETERS!
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$customer_id = $row['id']; // YOUR CUSTOMER ID
mysqli_free_result($result); // FREE RESULT SET
mysqli_close($conn); // CLOSE CONNECTION
And this would be the object oriented style:
$Name = "Dick";
$conn = new mysqli($server, $dbuser, $dbpw, $dbname);
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
$result = $conn->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
$customer_id = $row['id']; // YOUR CUSTOMER ID
$result->free(); // FREE RESULT SET
$conn->close(); // CLOSE CONNECTION
I would recommend naming your table something else than table since it's a reserved word and could get you into parsing problems. The same goes with field names. More reading: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
More about mysqli_fetch_array() and differences in procedural style and object oriented style use: http://php.net/manual/en/mysqli-result.fetch-array.php
$sql = "SELECT id FROM table WHERE '$Name' = table.first_name";
You simply need to concat the variable like this:
$sql = "SELECT id FROM table WHERE " . $Name . " = table.first_name";

check the manual that corresponds to your MySQL server version for the right syntax to use near ' masalah = 'dsd' AND gambar = 'kerusi.JPG'' at line 1

I want to add record to a table which contain a picture. when i try to add, it shows me this error
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 ' masalah = 'dsd' AND gambar = 'kerusi.JPG'' at line 1
so, here are the code
<?php
session_start();
include("Connections/connection.php");
$date = $_POST['date'];
$masalah = $_POST ['masalah'];
$gambar = $_POST ['gambar'];
$student_id = $_POST['student_id'];
$status = 'pending';
/*session yg di call tok lecturer tu*/
$student_id = "SELECT * FROM student WHERE student_id ='".$_SESSION['student_id']."'" ;
$result=mysql_query($student_id);
$getstudentid = mysql_fetch_assoc($result);
$student_id = $getstudentid['student_id'];
/*take 3 data from rc */
$sql = "SELECT * FROM aduan WHERE date = '$date', masalah = '$masalah' AND gambar = '$gambar' ";
$rr = mysql_query($sql) or die(mysql_error());
$tt = mysql_num_rows($rr);
if($tt > 0) {
header("Location: borang_aduan2.php?error=2");
} else { /*to check tarikh*/
$tarikh_user = strtotime($date);
$tarikh_harini = strtotime(date('Y-m-d'));
if($tarikh_user < $tarikh_harini) {
//error
header("Location: borang_aduan2.php?error=1");
} else {
//$No = $num_rows+1;
/*$sql_const = mysql_query ("Select MAX(user_name)as id from lecturer")or die (mysql_error());
$rows = mysql_fetch_array ($sql_const);
$id = $rows ['id'];*/
/*insert data*/
mysql_query("INSERT INTO aduan (date, masalah, gambar, student_id )
VALUES('$date','$masalah', '$gambar','$student_id')")
or die('Error: ' .mysql_error($conn));
echo "<script type='text/javascript'>
alert('Thanks make a report!')
location.href='borang_aduan2.php'
</script>";
}
//Freeing all memory associated with it
mysql_free_result($result);
//Closes specified connection
mysql_close($conn);
}
?>
Dont use "," in between two field selection criteria. So instead of:
SELECT * FROM aduan WHERE date = '$date',
^^
Use
SELECT * FROM aduan WHERE date = '$date' AND

MySQL Syntax Error with wpdb query/prepare

I'm simply trying to run a query using wpdb query and prepare statements.
On my page it returns:
[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 'myQuery' at line 1]
PHP Code:
<?php
$dirName1 = 'C:/wamp/www/c2c/wp-content/themes/flawless-v1-01';
$dirName2 = 'C:/wamp/www/c2c';
require_once($dirName1.'/config/setup.php');
require_once($dirName2.'/wp-config.php');
require_once($dirName2.'/wp-load.php');
$wpdb->show_errors();
$tableName = $wpdb->prefix . "user_orders";
$user = wp_get_current_user();
$userId = $user->ID;
$userName = $user->user_login;
// echo $tableName . ': ' . $userId . ': '. $userName;
// echo var_dump($tableName);
// echo var_dump($userId);
// echo var_dump($userName);
// These echo the correct formats for the prepare statement below
$myQuery = $wpdb->query(
$wpdb->prepare("
SELECT *
FROM $tableName
WHERE `user_id` = %d
AND `user_name` = %s",
$tableName,
$userId,
$userName)
);
$results = $wpdb->get_results(myQuery, ARRAY_A);
?>
You're missing the dollar sign before your variable name:
$results = $wpdb->get_results(myQuery, ARRAY_A);
should be
$results = $wpdb->get_results($myQuery, ARRAY_A);

Accessing data from database in joomla

I am using this query in Fabrik (Joomla Application) to pull the data from database, which is not working.
The same query with mysql syntax is working fine in PHPMYADMIN.
$db = FabrikWorker::getDbo(false, 2);
$query = $db->getQuery(true);
$query
->select('hostel_fee')
->from('hostel_fee AS a')
->join('INNER','students AS b ON (b.class = a.class)');
$db->setQuery($query);
$a = $db->loadResult();
return $a;
use the full queries like this
$db = &JFactory::getDBO();
$query = "SELECT m.id, m.title,m.level,mt.menutype FROM #__menu AS m
INNER JOIN #__menu_types AS mt ON mt.menutype = m.menutype
WHERE mt.menutype = m.menutype AND m.published = '1' ORDER BY mt.menutype,m.level";
$db->setQuery($query);
$rows = $db->loadObjectList();
OR
$rows = $db->loadResult();
Instead of using this
$db = FabrikWorker::getDbo(false, 2);
Use this.
$db =& JFactory::getDBO();
Or if you want to use any external database to connect to your extension you can use this
Connecting to an external database

MySQL to PDO error

I have the following sql code:
$statement = "`maildb`";
$query = "SELECT COUNT(*) as `num` FROM {$statement}";
$row = mysql_fetch_array(mysql_query($query));
I am trying to transform to PDO... I have tried the following:
$statement = "`maildb`";
$query = $db->prepare("SELECT COUNT(*) as `num` FROM {$statement}");
$query->execute();
$row = $query->fetchAll(PDO::FETCH_ASSOC);
That results in: Fatal error: Call to a member function prepare() on a non-object in function.php on line 8.
I have also tried this before, which should do the same:
$query = $db->prepare("SELECT * FROM maildb");
$query->execute();
$row = $query->fetchAll(PDO::FETCH_ASSOC);
$num = count($row);
Any help?
Thank you!!
You are using $db which is not initialized. Please initialize it first by doing $db = new PDO( <connection details here> ). See PDO manual on PHP.net for more details.
You probably didn't instantiate $db. Therefore, it's not an object, thus you can't call a function from it
I think the error lies in this line:
$query = $db->prepare("SELECT COUNT(*) as `num` FROM {$statement}");
Instead try using:
$query = $db->prepare("SELECT COUNT(*) as `num` FROM ?");
$query->execute($statement);