insert function and session into database - php

I have a query to insert but idk how to insert function and session into my database.
Thanks for helping!!
<?php
if (isset($_POST['submit'])){
$address = $_POST['address'];
$poscode = $_POST['poscode'];
$city = $_POST['city'];
$state = $_POST['state'];
$tel_no = $_POST['tel_no'];
$recipient = $_POST['recipient'];
$date = $_POST['date'];
$image = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
move_uploaded_file($image_tmp,"receipt/$image");
$insert_order = "insert into user_order(user_email,total_payment,address,poscode,city,state,tel_no,recipient,payment_status,image,date) values ('{$_SESSION['user_email']}','".total_price()."','$address','$poscode','$city','$state','$tel_no','$recipient','pending','$image','$date')";
$insert_order = mysqli_query($con, $insert_order);
if($insert_order){
echo "<script>alert('Order has been placed!')</script>";
echo "<script>window.open('user/my_account.php','_self')</script>";
}}
?>

Please add the this line after the php tag opening.
session_start();
Without starting the session you cant use the session variables in php.
This is works in my case. Please have a try.

Related

Trouble adding data from one table to another

I've been trying to make a payment page where after adding sufficient information, the order is taken with the items in the cart. for some reason, my code isn't responding and is showing a blank screen. I am relatively new to PHP. could anyone help me out? Here is the code below:-
<?php
require_once('../connection.php');
require_once('../product.php');
session_start();
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
if(isset($_POST['payment-made'])){
if(empty($name) || empty($email) || empty($address)){
echo "<script>alert('Please enter the complete information!');
window.location = 'orders.inc.php';</script>";
} else {
if(isset($_SESSION['Uid'])){
if(isset($_SESSION['cart'])){
$product_id = array_column($_SESSION['cart'], 'product_id');
$result = mysqli_query($conn,"SELECT * FROM `products`");
while($row = mysqli_fetch_assoc($result)){
foreach($product_id as $id){
if($row['product_id']==$id){
$ordername = $row['item_name'];
$nooforder = 1;
$sql1 = "INSERT INTO orders(order_name, no_order, cust_name, del_add)
VALUES ('$ordername', '$nooforder', '$name', '$email', '$address');";
$done = mysqli_query($conn, $sql1);
if($done){
echo "successful";
}
else{
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}
}
}
}
}
}
}
}
?>
I have doubts the problem lies within the $ordername variable. Could anyone clarify what's causing the problem and how to solve it?
Your INSERT query names four columns, but provides five variables to insert.

alert shows entry updated succesfully but entry does'nt update in database

I am creating a updating form page. it shows no error but there is no updation in my database.
I already check whole the database row names but that doesnt help.
that code is a little bit long but please help.
<?php
if (isset($_POST['update'])) {
$update_id = $_GET['edit_form'];
$bill = $_POST['b_no'];
$naam = $_POST['name'];
$mobile_no = $_POST['mobile'];
$addres = $_POST['add'];
$detail = $_POST['p_detail'];
$p_img_name = $_FILES['p_img']['name'];
$p_img_type = $_FILES['p_img']['type'];
$p_img_size = $_FILES['p_img']['size'];
$p_img_tmp = $_FILES['p_img']['tmp_name'];
$prc = $_POST['price'];
$deposite = $_POST['d_amt'];
$remaning = $_POST['r_amt'];
$b_img_name = $_FILES['b_img']['name'];
$b_img_type = $_FILES['b_img']['type'];
$b_img_size = $_FILES['b_img']['size'];
$b_img_tmp = $_FILES['b_img']['tmp_name'];
$p_date = date('y-m-d');
move_uploaded_file($p_img_tmp, "images/Product/$p_img_name");
move_uploaded_file($b_img_tmp, "images/Bill/$b_img_name");
$update_query = "UPDATE new_entry SET bill_no='$bill', name='$naam',
mobile_no='$mobile_no', address='$addres', product_detail='detail',
product_image='$p_img_name', price='$prc', deposite_amt='deposite',
remaining='$remaning', bill_image='$b_img_name',Product_date='$p_date'
WHERE s_no='$update_id' ";
if(mysqli_query($conn, $update_query)) {
echo "<script> alert('Entry Updated Successfully') </script>";
echo "<script> window.open('view_entry.php','_self') </script>";
} else {
echo "cant Update Entry.." .mysqli_error($conn);
}
}
?>

Displaying dynamic information php

Here is my code
<?php
session_start();
include 'header.php';
$getUserInfo = mysql_query("SELECT * FROM Campaigns WHERE cid = '$cid'");
$userinfo = mysql_fetch_assoc($getUserInfo);
$cid = $getUserInfo['cid']; //ID
$CName = $getUserInfo['CName']; //Name
$CDesc = $getUserInfo['CDesc']; //Description
$CAmt = $getUserInfo['CAmt']; //Rate
$CReqs = $getUserInfo['CReqs']; //Requirements
?>
<html>
<body>
<h3><?php echo $cid;?>, <?php echo $CName;?></h3>
<p><?php echo $CDesc;?>
</body>
</html>
And for every new item in the php database i want it to be displayed dynamically.
But for some reason none of the infomation is being displayed. Is it because i'm using getUserInfo
Replace
$cid = $getUserInfo['cid']; //ID
$CName = $getUserInfo['CName']; //Name
$CDesc = $getUserInfo['CDesc']; //Description
$CAmt = $getUserInfo['CAmt']; //Rate
$CReqs = $getUserInfo['CReqs']; //Requireme
with
$cid = $userinfo ['cid']; //ID
$CName = $userinfo ['CName']; //Name
$CDesc = $userinfo ['CDesc']; //Description
$CAmt = $userinfo ['CAmt']; //Rate
$CReqs = $userinfo ['CReqs']; //Requireme
You are reading from the wrong variable
Further suggestions:
Make sure you actually get a result from MySQL:
var_dump($getUserInfo);
var_dump($userinfo);
If not, check why not with echo mysql_error();
Make sure you have the right keys in your array (PHP variables are case sensitive)

web Development, PHP display variable from SQL as something else

Ok So i have an PHP page and i have a database.
In my database i have a table with a Field one of the fields is called accounttype it is enum('n', 'm', 's')
I am trying to display on my PHP page if the user is N it should say Normal User if the user is E Expert user or S Super user...
How do i go about doing this?
Top of PHP Page
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$phone = $row["phone"];
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
}
?>
Where i am trying to display on the page This is the code. Right now it just puts a blank space.
<span class="admin">Edward</span>
<span class="date">March 19, 2048</span>
<span class="tag"><?php echo "$accounttype"; ?></span>
<span class="comment">166 comments</span>
picture
http://i.stack.imgur.com/KXu9A.png
first make a connection, than dont make a while, make a if like this
if($row = mysql_fetch_array($sql)){
$phone = $row["phone"];
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
}
and than
$speaking_type = null;
switch($accounttype) {
case 'n':
$speaking_type = 'Normal User';
break;
case 'm':
$speaking_type = 'Expert User';
break;
case 's':
$speaking_type = 'Super User';
break;
default:
$speagink_type = 'none';
//throw new Exception('unsupported account type '.$accounttype);
}
echo $speaking_type;
I think the problem is your scope. Your variables are defined within the while-loop, and so they are unknown further in the document. Try instantiating them on top (before the while-loop) like this:
$phone = null;
$country = null;
$state = null;
$city = null;
$accounttype = null;
$bio = null;
Than the variables will be known outside the while and the values will be remembered when you print them.
I thought u didn't connect to the database first .use following code to the connect with your credentials.That's why you are seeing a blank space
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);

Using $_SESSION to carry data

I have attempted to use $_SESSION in a form input I am creating however I cannot get it to work and do not know what I am doing wrong, it works with my previous part of the form when carrying data over to the next page - however the code does not seem to work for the main part of the form.
<?php
//This includes the variables, adjusted within the 'config.php file' and the functions from the 'functions.php' - the config variables are adjusted prior to anything else.
require('configs/config.php');
require('configs/functions.php');
//Check to see if the form has been submited, if it has we continue with the script.
if(isset($_POST['confirmation']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['address1']) && isset($_POST['city']) && isset($_POST['postcode']) and $_POST['confirmation']=='true')
{
//Slashes are removed, depending on whether magic_quotes_gpc is on.
if(get_magic_quotes_gpc())
{
$_POST['name'] = stripslashes($_POST['name']);
$_POST['email'] = stripslashes($_POST['email']);
$_POST['address1'] = stripslashes($_POST['address1']);
$_POST['address2'] = stripslashes($_POST['address2']);
$_POST['city'] = stripslashes($_POST['city']);
$_POST['postcode'] = stripslashes($_POST['postcode']);
$_POST['phonenum'] = stripslashes($_POST['phonenum']);
}
//Create the future reference number of the repair.
$maxid = mysql_fetch_array(mysql_query('select max(id) as id from repairs'));
$id = intval($maxid['id'])+1;
//Create the future reference number of the repair.
$maxref = mysql_fetch_array(mysql_query('select max(reference) as reference from repairs'));
$reference = intval($maxref['reference'])+8;
//Here the session variables are converted back into standard variables.
$model = $_SESSION['model'];
$problem = $_SESSION['problem'];
$info = $_SESSION['info'];
$device = $_SESSION['device'];
$price = $_SESSION['price'];
$image = $_SESSION['image'];
//Here the variables are protected using mysql_real_escape_string.
$name = mysql_real_escape_string(substr($_POST['name'],0,150));
$email = mysql_real_escape_string(substr($_POST['email'],0,255));
$address1 = mysql_real_escape_string(substr($_POST['address1'],0,255));
$address2 = mysql_real_escape_string(substr($_POST['address2'],0,255));
$city = mysql_real_escape_string(substr($_POST['city'],0,100));
$postcode = mysql_real_escape_string(substr($_POST['postcode'],0,9));
$phonenum = mysql_real_escape_string(substr($_POST['phonenum'],0,11));
$date = date("r");
//Here the variables are protected using trim.
$name = trim($name);
$email = trim($email);
$address1 = trim($address1);
$address2 = trim($address2);
$city = trim($city);
$postcode = trim($postcode);
$phonenum = trim($phonenum);
//Here the variables are protected using htmlspecialchars.
$name = htmlspecialchars($name);
$email = htmlspecialchars($email);
$address1 = htmlspecialchars($address1);
$address2 = htmlspecialchars($address2);
$city = htmlspecialchars($city);
$postcode = htmlspecialchars($postcode);
$phonenum = htmlspecialchars($phonenum);
//Here the variables are protected using strip_tags.
$name = strip_tags($name);
$email = strip_tags($email);
$address1 = strip_tags($address1);
$address2 = strip_tags($address2);
$city = strip_tags($city);
$postcode = strip_tags($postcode);
$phonenum = strip_tags($phonenum);
//The details about the repair are entered into the database
$query = mysql_query("insert into repairs (id, model, problem, info, name, email, address1, address2, city, postcode, phonenum, price, date, reference) values ('$id', '$model', '$problem', '$info', '$name', '$email', '$address1', '$address2', '$city', '$postcode', '$phonenum', '$price', '$date', '$reference')") or die(header('Location: 404.php'));
?>
Some HTML is here.
<?
}
else {
header('Location: 404.php');
}
?>
Can anyone help me to get this to work?
You have to initiate your session in the beginning of your script with session_start()
set your error logging to the most verbose level. If your Paste is exact, you have some spaces in the beginning which cause, that you cant send headers anymore and so you cant initiate the session.

Categories