How do I use an include_path with an insert query - php

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

Related

i am applying this sql compersion query but receiving syntax error

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

php script wont add record to mysql

The first example will add data to mysql database without any issue. The second block of code - where I try to use variables wont. Can someone please explain where I am going wrong?
<?php
$query = "INSERT INTO subjects (menu_name,position,visible) VALUES ('Edit me',4,1)";
$result = mysqli_query($connection, $query);
Problem CODE:
<?php
$menu_name = "TEST";
$position = 5;
$visible = 1;
$query = "INSERT INTO subjects (menu_name,position,visible)
VALUES ('{menu_name}',{position}, {visible})";
$result = mysqli_query($connection, $query);
*Answer updated with MySQLi prepare statement, thanks #h2ooooooo
<?php
//Open a new connection to the MySQL server
$db = new mysqli('host','username','password','database_name');
//Output connection errors
if ($db->connect_error) {
die('Error : ('. $db->connect_errno .') '. $db->connect_error);
}
$sql = "INSERT INTO subjects (menu_name, position, visible) VALUES (?, ?, ?)";
if (!$stmt = $db->prepare($sql)) {
echo 'Database prepare error';
exit;
}
$stmt->bind_param('sss', $menu_name, $position, $visible);
if (!$stmt->execute()) {
echo 'Database execute error';
exit;
}
$stmt->close();
I'd say for you to take a look in the many tutorials thorugh net, like these:
http://markonphp.com/simple-insert-mysqli/ and
http://www.sanwebe.com/2013/03/basic-php-mysqli-usage
$query = "INSERT INTO subjects (menu_name,position,visible) VALUES
('".$menu_name."','".$position."', '".$visible."')";
try this

Insert values with if condition with mysqli

I am trying to make register member page. If a new member insert an email which has been already exist, then there will be a notification saying that the email is exist. But if the email has not been exist, the values they insert in the form will be send to database.
I don't know what is wrong with my code bellow. It just blank and doesn't send anything to databse. I need a help.
<?php
//conection:
$link = mysqli_connect(".com","klaudia","intheclaud","elektro") or die("Error " . mysqli_error($link));
//consultation:
$member_id=$_GET['member_id'];
$member_name=ucwords(htmlspecialchars($_POST['member_name']));
$member_email=$_POST['member_email'];
$member_password=htmlspecialchars($_POST['member_password']);
$member_phone=$_POST['member_phone'];
$member_address_satu=ucwords(htmlspecialchars($_POST['member_address_satu']));
$member_address_dua=ucwords(htmlspecialchars($_POST['member_address_dua']));
$member_reference=$_POST['member_reference'];
$query = "SELECT * FROM member_registry WHERE member_email='$member_email '" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = $link->query($query);
if (mysqli_num_rows($result) > 0) {
echo "This email you are using has been registered before";
}
else {
mysqli_query($link, "INSERT INTO member_registry (
'member_id',
'member_name',
'member_email',
'member_password',
'member_phone',
'member_address_satu',
'member_address_dua',
'member_reference')
VALUES (0,1,2,3,4,5,6,7,8)";
?>
I have tried to check the connection and the database. Everything works fine here. When I insert someone name which has been in the table of the database, it will echo that the email already exist. and vice versa.
$query = "SELECT * FROM member_registry WHERE member_name='Klaudia '" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = $link->query($query);
if (mysqli_num_rows($result) > 0) {
echo "This email you are using has been registered before";
}
else {
echo "This email you are using has NOT been registered before";
}
[UPDATE]
mysqli_query($link, "INSERT INTO member_registry (
'member_id',
'member_name',
'member_email',
'member_password',
'member_phone',
'member_address_satu',
'member_address_dua',
'member_reference')
VALUES (0,1,2,3,4,5,6,7,8)");
}
?>
You shouldn't handle this by two separate queries (at least without a transaction).
Instead create a unique index that doesn't allow the same email address twice in the table and check for the specific ER_DUP_ENTRY error code to detect doublets.
sscce:
<?php
define('MYSQL_ER_DUP_ENTRY', 1062);
$mysqli = new mysqli('localhost', 'localonly', 'localonly', 'test');
if ($mysqli->connect_errno) {
trigger_error('connection failed', E_USER_ERROR);
}
$result = $mysqli->query('
CREATE TEMPORARY TABLE soFoo (
id int auto_increment,
email varchar(128),
primary key(id),
unique key(email)
)'
);
if ( !$result) {
trigger_error('create table failed', E_USER_ERROR);
}
$stmt = $mysqli->prepare('INSERT INTO soFoo (email) VALUES (?)');
if (!$stmt) {
trigger_error('prepare failed', E_USER_ERROR);
}
$result = $stmt->bind_param("s", $email);
if ( !$result) {
trigger_error('bind_param failed', E_USER_ERROR);
}
foreach( array('email1', 'email2', 'email1') as $n=>$email ) {
echo $n, ' ', $email;
$result = $stmt->execute();
if ( $result ) {
echo " ok\r\n";
}
else {
if ( MYSQL_ER_DUP_ENTRY==$stmt->errno ) { // <-- here's the test for the duplicate entry
echo " duplicate\r\n";
}
else {
var_dump($stmt->errno, $stmt->error);
}
}
}
prints
0 email1 ok
1 email2 ok
2 email1 duplicate
You have an error in your syntax. You don't close your function.
Also you shouldn't use single quotes around your coumn names.
mysqli_query($link, "INSERT INTO member_registry (
`member_id`,
`member_name`,
`member_email`,
`member_password`,
`member_phone`,
`member_address_satu`,
`member_address_dua`,
`member_reference`)
VALUES (0,1,2,3,4,5,6,7,8)");

ODBC/MYSQL Insert a Query Result from ODBC to a databse in MYSQL

I'm connecting to a cloud database through an ODBC connection:
$conn = odbc_connect('MYDATABASE','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$sql = "SELECT DATETIME_ID, NAME, Sum(CNDROP) AS DATA
FROM MY_TABLE
WHERE DATETIME_ID>='2014-09-28:00:00:00'
and DATETIME_ID<='2014-09-28 23:00:00'
and NAME IN ('CC2')
GROUP BY DATETIME_ID, NAME ORDER BY DATETIME_ID, NAME";
$rs = odbc_exec($conn,$sql);
if (!$rs) {
exit("Consulta fallida");
}
$result = odbc_exec($conn,$sql) or die(exit("Error en odbc_exec"));
print odbc_result_all($result,"border=1");
odbc_close($conn);
I can get the data, and print the data, but now I need insert that data into a MySQL database into my computer.
I don't have any idea how to do it, so I need help with an example. I tried to search on google but nothing was helpful.
Option 1:
Function to SELECT
function get_data_from_cloud(){
$conn=odbc_connect('CLOUD','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$sql="SELECT DATETIME, NAME, CNDROP
FROM TABLE1
WHERE DATETIME>='2014-09-28 00:00:00' and
DATETIME<='2014-09-28 23:00:00' and
NAME IN ('PETER')
GROUP BY DATETIME, NAME
ORDER BY DATETIME, NAME";
$result=odbc_exec($conn,$sql)or die(exit("Error en odbc_exec"));
$data = array();
while (odbc_fetch_row($result)) {
$data[]=array('DATETIME' => odbc_result ($result, "DATETIME"),
'NAME'=> odbc_result ($result, "NAME"),
'CNDROP'=> odbc_result ($result, "CNDROP"));
}
return $data;
}
Function to INSERT
function insert_cloud_data($cloud_data=array()){
$conn=odbc_connect('LOCAL','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
foreach($cloud_data as $data){
$sql = sprintf("INSERT INTO Prueba (DATIME, NAME, CNDROP)
VALUES ( '%s','%s','%s')",
$data['DATETIME'], $data['NAME'], $data['CNDROP']);
$rs = odbc_exec($conn,$sql);
if (!$rs) {
error_log("Consulta fallida");
}
}
odbc_close($conn);
}
Option 2:
Function to SELECT
function get_data_from_cloud(){
$conn=odbc_connect('CLOUD','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$sql="SELECT DATETIME, NAME, CNDROP
FROM TABLE1
WHERE DATETIME>='2014-09-28 00:00:00' and
DATETIME<='2014-09-28 23:00:00' and
NAME IN ('PETER')
GROUP BY DATETIME, NAME
ORDER BY DATETIME, NAME";
$result=odbc_exec($conn,$sql)or die(exit("Error en odbc_exec"));
$data = array();
while (odbc_fetch_row($result)) {
$data[]=array(odbc_result ($result, "DATETIME"),
odbc_result ($result, "NAME"),
odbc_result ($result, "CNDROP"));
}
return $data;
}
Function to INSERT
function insert_cloud_data($cloud_data=array()){
$conn=odbc_connect('LOCAL','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$sql = "INSERT INTO Prueba (DATIME, NAME, CNDROP)
VALUES (?, ?, ?)";
$stmt = odbc_prepare($conn, $sql);
if(!$stmt) die("could not prepare statement ".$sql);
foreach($cloud_data as $data){
odbc_execute($stmt, $data);
}
odbc_close($conn);
}
USAGE
$cloud_data = get_data_from_cloud();
insert_cloud_data($cloud_data);
Here is different approach.
Create $conn1 (cloud) and $conn2 (localhost). Query the $conn1 then use php while and insert command into $conn2.
$conn1 = (cloud);
$conn2 = (localhost);
$query = "SELECT ...";
$result = odbc_exec($conn1,$query);
while( fetch result data ) {
$query = "INSERT ....";
odbc_exec($conn2,$query);
}

data Not Storing

this is my connection class
class Connection {
public function query($sql){
mysql_connect('localhost','root','') or die("Connection error ". mysql_error());
mysql_select_db('liontours') or die("Database error ". mysql_error());
$results = mysql_query($sql);
$last_inserted_id = mysql_insert_id();
return array('results'=>$results, 'last_id'=>$last_inserted_id);
}
}
this is my model
public function V_reg($v_no, $dl_no, $owner, $o_name, $o_nic, $i_date, $ex_date, $p_report, $nic, $s_name, $f_name, $initials, $dob, $stat, $v_type) {
$sql = "INSERT INTO `vehicledetails`(`vehicle_no`, `owner`, `owner_name`, `owner_nic`, `insured_date`, `ex_date`, `police_report`,`type`)
VALUES ('$v_no','$owner','$o_name','$o_nic','$i_date','$ex_date','$p_report','$v_type')";
$conn = new Connection();
//vehicle id of last inserted record
$vehicle_id = mysql_insert_id();
$results = $conn->query($sql);
$last_vehicle_record_id = $results['last_id'];
$fk_key = $last_vehicle_record_id;
//checking the first table insert successful if so do the second insert else must define counter measure in else part
if ($fk_key !== 0) {
$sql1 = "INSERT INTO driverdetails (id, vehicle_id, nic,sir_name,first_name,dlNo,initials,dob,status )
VALUES ('null', '$fk_key', '$nic','$s_name','$f_name','$dl_no', '$initials','$dob','$stat')";
$results = $conn->query($sql1);
}else{
die('transaction failed').mysql_error();
}
return $results;
}
}
i don't know what is wrong with this coding but it's not passing the data all the time it says transaction failed and no error given. just the text can some body tell me any thing wrong with this coding ? if there is no coding errors is it a problem with my database ?

Categories