i am applying this sql compersion query but receiving syntax error - php

this is my code at insertion its working but at the time of comparison its showing parse syntax error
// attempt insert query execution
$sql = "INSERT INTO availer (source_name, dstntn_name, sou_date, flight_no, sou_weight, contact_name, contact_no) VALUES ('$source_name', '$dstntn_name', '$sou_date', '$flight_no', '$sou_weight', '$contact_name', '$contact_no')";
if(mysqli_query($link, $sql)){
$sql ="SELECT availer.id, availer.source_name,availer.dstntnn_name,availer.sou_date,availer.sou_weight FROM availer availer
WHERE EXISTS (SELECT * FROM provider provider
WHERE provider.source_name = availer.source_name AND provider.dstntn_name = availer.dstntn_name AND provider.sou_date = availer.sou_date AND provider.flight_no = availer.flight_no");
print $query;
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

You missplaced the " mark. Your code should be like this.
$sql = "INSERT INTO availer (source_name, dstntn_name, sou_date, flight_no, sou_weight, contact_name, contact_no) VALUES ('$source_name', '$dstntn_name', '$sou_date', '$flight_no', '$sou_weight', '$contact_name', '$contact_no')";
if(mysqli_query($link, $sql)){
$sql ="SELECT availer.id, availer.source_name,availer.dstntnn_name,availer.sou_date,availer.sou_weight FROM availer availer
WHERE EXISTS (SELECT * FROM provider provider
WHERE provider.source_name = availer.source_name AND provider.dstntn_name = availer.dstntn_name AND provider.sou_date = availer.sou_date AND provider.flight_no = availer.flight_no)";
print $query;
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "prog_db");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$source_name = mysqli_real_escape_string($link, (isset($_POST['source'])));
$dstntn_name = mysqli_real_escape_string($link,(isset($_POST['dstntn'])));
$sou_date = mysqli_real_escape_string($link,(isset($_POST['date'])));
$flight_no = mysqli_real_escape_string($link,(isset($_POST['flightno'])));
$sou_weight = mysqli_real_escape_string($link,(isset($_POST['weight'])));
$contact_name = mysqli_real_escape_string($link,(isset($_POST['name'])));
$contact_no = mysqli_real_escape_string($link,(isset($_POST['contact'])));
// attempt insert query execution
$sql = "INSERT INTO availer (source_name, dstntn_name, sou_date, flight_no, sou_weight, contact_name, contact_no) VALUES ('$source_name', '$dstntn_name', '$sou_date', '$flight_no', '$sou_weight', '$contact_name', '$contact_no')";
if(mysqli_query($link, $sql)){ $sql ="SELECT availer.id, availer.source_name,availer.dstntnn_name,availer.sou_date,availer.sou_weight FROM availer availer
WHERE EXISTS (SELECT * FROM provider provider
WHERE provider.source_name = availer.source_name AND provider.dstntn_name = availer.dstntn_name AND provider.sou_date = availer.sou_date AND provider.flight_no = availer.flight_no)";
$result = mysql_query($sql,$link);
while($row = mysql_fetch_array($result)) {
echo $row['SOURCE_name'];
}
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
but i am not able to fatch the result on the webpage .

You should write this
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if (isset ($_POST['source'], $_POST['dstntn'], $_POST['date'], $_POST['flightno'],$_POST['weight'],$_POST['name'], $_POST['contact'])){
$source_name = mysqli_real_escape_string($link,$_POST['source']);
$dstntn_name = mysqli_real_escape_string($link,$_POST['dstntn']);
$sou_date = mysqli_real_escape_string($link,$_POST['date']);
$flight_no = mysqli_real_escape_string($link,$_POST['flightno']);
$sou_weight = mysqli_real_escape_string($link,$_POST['weight']);
$contact_name = mysqli_real_escape_string($link,$_POST['name']);
$contact_no = mysqli_real_escape_string($link,$_POST['contact']);
// attempt insert query execution
$sql = "INSERT INTO availer (source_name, dstntn_name, sou_date, flight_no, sou_weight, contact_name, contact_no) VALUES ('$source_name', '$dstntn_name', '$sou_date', '$flight_no', '$sou_weight', '$contact_name', '$contact_no')";
if(mysqli_query($link, $sql)){ $sql ="SELECT availer.id, availer.source_name,availer.dstntnn_name,availer.sou_date,availer.sou_weight FROM availer availer
WHERE EXISTS (SELECT * FROM provider provider
WHERE provider.source_name = availer.source_name AND provider.dstntn_name = availer.dstntn_name AND provider.sou_date = availer.sou_date AND provider.flight_no = availer.flight_no)";
$result = mysql_query($sql,$link);
while($row = mysql_fetch_array($result)) {
echo $row['SOURCE_name'];
}
}
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
// close connection
mysqli_close($link);

Related

Insert query is not working "Query was empty" I need to insert the data that is displayed on the table

if(isset($_POST['insert']))
{
$index=1;
while($index <= $count) ##inserting the values in db
{
$loan = $_POST["loan_amount$index"];
$interest1 = $_POST["int1$index"];
$interest2 = $_POST["int2$index"];
$interest3 = $_POST["int3$index"];
$interest4 = $_POST["int4$index"];
$interest5 = $_POST["int5$index"];
$interest6 = $_POST["int6$index"];
$interest7 = $_POST["int7$index"];
$interest8 = $_POST["int8$index"];
$interest9 = $_POST["int9$index"];
$interest10 = $_POST["int10$index"];
$forOdNextMonth = $_POST["due1$index"];
$forOdNextMonth1 = $_POST["due2$index"];
$forOdNextMonth2 = $_POST["due3$index"];
$forOdNextMonth3 = $_POST["due4$index"];
$forOdNextMonth4 = $_POST["due5$index"];
$forOdNextMonth5 = $_POST["due6$index"];
$forOdNextMonth6 = $_POST["due7$index"];
$forOdNextMonth7 = $_POST["due8$index"];
$forOdNextMonth8 = $_POST["due9$index"];
$forOdNextMonth9 = $_POST["due10$index"];
$total= $_POST["total$index"];
insert query:
$insertsql = "INSERT INTO `members_details`
(loan_amount,int1,int2,int3,int4,int5,int6,int7,int8,int9,int10,due1,due2,due3, due4, due5,due6,due7,due8,due9,due10,total)
VALUES(
'".$loan."',
'".$interest1."',
'".$interest2."',
'".$interest3."',
'".$interest4."',
'".$interest5."',
'".$interest6."',
'".$interest7."',
'".$interest8."',
'".$interest9."',
'".$interest10."',
'".$forOdNextMonth."',
'".$forOdNextMonth1."',
'".$forOdNextMonth2."',
'".$forOdNextMonth3."',
'".$forOdNextMonth4."',
'".$forOdNextMonth5."',
'".$forOdNextMonth6."',
'".$forOdNextMonth7."',
'".$forOdNextMonth8."',
'".$forOdNextMonth9."',
'".$total."')";
mysql_query($insertsql) or die(mysql_error());
$index++; //incrementing index
}
}
This is the code that shows the error.
mysql_query is deprecated from PHP 5.5 onwards - http://php.net/manual/en/migration55.deprecated.php.
Please use mysqli.
To connect to db using mysqli and php:
<?php
$conn = mysqli_connect('localhost', 'username', 'password', 'database');
To run queries:
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Regarding your error, try running the queries using mysqli.

I have the information, how do I get it into my db?

I figured out how to run Arp-scan and show the results on the page. Now I need to insert the information into a Mysql DB. I know how to insert a single record, but I struggle with how to get many records into the db. Can't seem to wrap my head around it. Here's my code
<?php
$link = mysqli_connect("localhost", "user", "password", "mydb");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
error_reporting(E_ALL);
ini_set('display_errors', '1');
$output = shell_exec("sudo arp-scan --interface=eth0 --localnet --numeric -- quiet| grep -e 80:82:87");
foreach (explode("\n", $output) as $host)
{
list($ip, $mac) = array_pad(explode("\t", $host),2,null);
echo "$ip $mac<br>";
}
// attempt insert query execution
$sql = "INSERT INTO phones (ipadd, mac) VALUES ('$ip', '$mac')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
Any help is greatly appreciated
<?php
$values = array();
foreach (explode("\n", $output) as $host)
{
list($ip, $mac) = array_pad(explode("\t", $host),2,null);
$values[] = '("'.$ip.'", "'.$mac.'")';
}
// attempt insert query execution
$sql = "INSERT INTO phones (ipadd, mac) VALUES ".implode(',', $values);
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
It will create an array of strings in following format:
("IP", "MAC")
and connect them later using a comma
("IP", "MAC"),("IP2", "MAC2")
and add into the query

Query doesn't insert value into DB

In my query the update statement doesn't work, the error given is:
Number of parameter doesn't match with prepared statement
this is my code:
public function update_resource($resource)
{
$mysqli = new MySQLi(HOST, USERNAME, PASSWORD, DATABASE);
$this->connection_state($mysqli);
$id = $resource['id'];
$descrizione = $resource['descrizione'];
$sigla = $resource['sigla'];
$colore = $resource['colore'];
$planning = $resource['planning'];
try
{
$query = "UPDATE risorse SET descrizione = '$descrizione'
AND sigla = '$sigla' AND colore = '$colore' AND planning = '$planning'
WHERE id = '$id' ";
$stmt = $mysqli->prepare($query);
$stmt -> bind_param("ssssi", $descrizione, $sigla, $colore, $planning, $id);
echo $query;
if($stmt->execute())
{
echo "Added!";
}
else
{
echo "Err: " . $stmt->error;
}
}catch(Exception $e){ echo $e->getMessage(); }
}
The code go into the Added condition but the query fail, what's the problem?
public function update_resource($resource)
{
$mysqli = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$id = $resource['id'];
$descrizione = $resource['descrizione'];
$sigla = $resource['sigla'];
$colore = $resource['colore'];
$planning = $resource['planning'];
try
{
$query = "UPDATE risorse SET descrizione = '$descrizione'
, sigla = '$sigla', colore = '$colore', planning = '$planning'
WHERE id = '$id' ";
$stmt = $mysqli->prepare($query);
$stmt -> bind_param($descrizione, $sigla, $colore, $planning, $id);
echo $query;
if($stmt->execute())
{
echo "Added!";
}
else
{
echo "Err: " . $stmt->error;
}
}catch(Exception $e){ echo $e->getMessage(); }
}?
Your problem is that you don't have any placeholders in your query.
Refer to manual to see how placeholders should be set.
In general, placeholders are ? which later will be replaced with values, so your query should look like:
$query = "UPDATE risorse SET descrizione = ?
AND sigla = ? AND colore = ? AND planning = ?
WHERE id = ?";
please visit on http://php.net/manual/en/pdostatement.bindparam.php.you got your answer.see Example #1 Execute a prepared statement with named placeholders

How do I use an include_path with an insert query

The program works everything is fine and running and even inserting the information. However, when you click the register button it displays the goodReg.inc file and puts the die message for the mysqli_query afterwards..So basically it says that the User registered successfuly, but couldn't connect to the login server.
URL = www.cameronwebsites.com/php/lesson5/register.php
My Code:
<?php
/* connection info */
ini_set("include_path","../../includes");
include("dbinfo.inc");
$cxn = mysqli_connect($host,$user,$password,$dbname)
or die("Couldn't connect to server.");
?>
foreach($good_data as $field => $value)
{
$good_data[$field] = mysqli_real_escape_string($cxn,$value);
}
$sql = "INSERT INTO UserInfo (user_id, password, first_name, last_name, city, country, email) VALUES ('$good_data[user_id]', '$good_data[password]', '$good_data[first_name]', '$good_data[last_name]', '$good_data[city]', '$good_data[country]', '$good_data[email]')";
include('goodReg.inc');
$result = mysqli_query($cxn,$sql) or die("<p>**Couldn't connect to login server**</p>");
$row = mysqli_fetch($result);
if ($row > 0)
{
$sql2 = "UPDATE TimeStamp SET time = CURRENT_TIMESTAMP where user_id='$good_data[user_id]'";
$result2 = mysqli_query($cxn,$sql2) or die("<p>Couldn't Connect to Login</p>");
}
else
{
echo $message;
extract($good_data);
include('register.inc');
exit();
}
}
}
else
{
include("register.inc");
}
The $result = mysqli_query is where the connection dies, but not sure why
I needed to get rid of the mysqli_fetch function as you cannot use that function with an INSERT query.
$sql = "INSERT INTO UserInfo (user_id, password, first_name, last_name, city, country, email) VALUES ('$good_data[user_id]', '$good_data[password]', '$good_data[first_name]', '$good_data[last_name]', '$good_data[city]', '$good_data[country]', '$good_data[email]')";
mysqli_query($cxn, $sql) or die ("Couldn't insert into UserInfo: " . mysqli_error($cxn));
$sql2 = "UPDATE TimeStamp SET time = CURRENT_TIMESTAMP where user_id='$good_data[user_id]'";
$result2 = mysqli_query($cxn,$sql2) or die("Couldn't update TimeStamp: " . mysqli_error($cxn));

Insert and Update on one click

I have two table Billing_month and registration in mysql. i want to insert into Billing_month and at the same time i want to update a row (Arrears) in registration table. how can i do this.
PHP Code is:
<?php
//create connection
include("database/db.php");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Insert Data
#$month=$_POST['Bill_month'];
#$issuedate=$_POST['Bill_issue_date'];
#$duedate=$_POST['Bill_due_date'];
#$surcharge =$_POST['Surcharge'];
$sql = "INSERT INTO billing_month (Bill_month,Bill_issue_date,Bill_due_date,Surcharge) values('$month','$issuedate','$duedate','$surcharge')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
Need your help?
I would only run the query update if the insert was successful.
<?php
//create connection
include("database/db.php");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Insert Data
#$month=$_POST['Bill_month'];
#$issuedate=$_POST['Bill_issue_date'];
#$duedate=$_POST['Bill_due_date'];
#$surcharge =$_POST['Surcharge'];
$sql = "INSERT INTO billing_month (Bill_month,Bill_issue_date,Bill_due_date,Surcharge)
values('$month','$issuedate','$duedate','$surcharge')";
if(mysqli_query($link, $sql)){
$res = "Records added successfully.";
// Update Registration Arrears query
$sql_upadte = "Update registration ...."
if(mysqli_query($link, $sql_update)){
$res .= "<br>Registration record updated successfully.";
}else{
$res .= "<br>ERROR: Could not able to execute.<br>".$sql_update."<br>".mysqli_error($link);
}
echo($res);
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
Use two queries to insert and to update, then execute them
$sql = "INSERT INTO billing_month (Bill_month,Bill_issue_date,Bill_due_date,Surcharge)
values('$month','$issuedate','$duedate','$surcharge')";
$sqlUpdate = "Place your update query here";
$insert = mysqli_query($sql);
$update = mysqli_query($sqlUpdate);
After insert into your billing table get last inserted id
$id = mysql_insert_id();
Then update your registration table.
$query = "UPDATE registration SET .... WHERE billing_id = $id; ";
mysql_query($query);
This is a simple multi query, you can look at php - multi_query here.
<?php
$query = "INSERT INTO billing (...) VALUES (....) ; ";
$query .= "UPDATE billing SET .... ; ";
if(!mysqli_multi_query($link,$query)){
...
}
Otherwise you can still do just do two single queries and execute them one after the other

Categories