print passes values - php

i am using insert into db(fname,lname) values ('$fname','$lname')
command i want that when this query passes on the nest page it show me which fname and which lname is passed or added like this statement
Following recorded is updated in database successfully
first name = abc
last name = xyz

Store the query in a variable, execute and print it out:
$query = "insert into db(fname,lname) values ('$fname','$lname')";
mysql_query($query);
echo $query;
Or, if you want to print just the variables, why can't you just do:
echo 'firstname: '.$fname.' ';
echo 'lastname: '.$lname;
If this is not what you needed, please clarify your question.

$query = "insert into db(fname,lname) values ('$fname','$lname')";
if(mysql_query($query))
{
echo "Following recorded is updated in database successfully\n";
echo "first name = $fname \n";
echo "last name = $lname \n";
}

Related

Show logged in user's data in profile.php

<?php
require('db.php');
$_SESSION['sloggedIn']="yes";
$data1['first_name'] = $_SESSION['sfirstname'];
$data1['email'] = $_SESSION['semail'];
$sqlQuery = "SELECT first_name, email FROM otium where id = :id";
file_put_contents('log/DBErrors.txt', 'Connection: '.'rivi8'.$sqlQuery."\n", FILE_APPEND);
$kysely1 = $DBH->prepare($sqlQuery);
$kysely1->execute($data1);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysqli_fetch_array
//while($row = mysqli_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
echo $sqlQuery['first_name'] . "<br />";
echo $sqlQuery['email'] . "<br />";
//}
?>
First time doing this php and i would like to print out user's logged in data, i have this code but is not working! please help
$sqlQuery is just string variable containing query, you can't expect it to be a array.
I guess you already got firstName and email from session then there is no need of executing sql query, just printing the session will make your job done.
echo $_SESSION['first_name'] . "<br />";
echo $_SESSION['email'] . "<br />";

Echo specific row from mysql database, within set of range

I tried to echo rows from mysql database. So in my code user can enter a value, and then program echoes table around the entered value. That part of the code works fine, but when I tried to echo entered values and corresponding row to the value, it somehow gave me first row. While I needed exact row, which I previously entered.
Here is my code:
$density = $_POST["density"];
$t = $_POST["t"];
$query = $pdo->prepare ("SELECT * FROM mytable WHERE "
."(Density >= '$density'-0.05 AND Density <='$density' +0.05) AND "
."(Temp >='$t' -2 AND Temp <='$t' +2) ");
if (!$query->rowCount() == 0) {
$query -> execute();
while ($results = $query->fetch(PDO::FETCH_ASSOC)){
echo "<table>";
echo "<tr><td>";
echo $results['Temperature'];
echo "</td><td">";
echo $results['Density'];
echo "</td><td>";
echo $results['DensityValues'] + $delden;
echo "</td></tr>";
echo "</table>";
}
}
So currently it prints the first row which is: Temp=>10, Density=>600, DensityValues=>658;
While I need to get values I have enterd and coresponding density value: Temp=>12, Density=>650, DensityValues=>678;

PHP: How to store cart data (foreach loop) into different variables to store in MySQL

Good afternoon,
I have searched for an answer to this query and the closest I could find was this: Storing elements in an array at each iteration of a foreach on PHP
I am building my own eCommerce platform using PHP and I have gotten to the point now where I can add items to my cart.
I am currently building the checkout page but I dont know how to store each product ordered into different variables so that I can store them into MySQL.
The following code allows me to populate the relevant data I just dont know how to store this data into the variables:
<?php
session_start();
include '../connection.php';
$cartProducts = array();
foreach ($_SESSION["cart_item"] as $item){
echo "Product Details Acquired:" . "<br>";
echo $item["product_name"] . "<br>";
echo $item["quantity"] . "<br>";
echo "£".$item["product_price"] . "<br> <br>";
}
//setting username variable
$myusername = $_SESSION['login_user'];
// getting client info
include '../connection.php';
$sql="SELECT id, username, phone, email from clients WHERE username='$myusername'";
if ($result = mysqli_query($connection, $sql)){
//Presenting data from array
while ($row = mysqli_fetch_array($result)) {
$client_id= $row['id'];
$client_phone= $row['phone'];
$client_user= $row['username'];
$client_email= $row['email'];
echo "User details acquired" ."<br>";
echo $client_id ."<br>";
echo $client_phone ."<br>";
echo $client_user ."<br>";
echo $client_email ."<br>";
}
}
else {
echo "No client data found...";
}
?>
This produces all of the data i need:
Product Details Acquired:
Chair
1
£129.89
Product Details Acquired:
Double Bed
1
£1999
User details acquired
4
2147483647
coreyhowe12
corey#test.com
Would appreciate any help :)
Why don't you use INSERT query in your foreach loop like this:
foreach ($_SESSION["cart_item"] as $item){
// use your product attributes like this.
$product_name = $item["product_name"];
$quantity = $item["quantity"];
// then use insert query like this
$insert = "INSERT INTO products (`product_name`, .. other columns ..) VALUES ('$product_name', .. other column values ..)";
mysqli_query($connection, $insert);
}
Can you not store it in an array and then loop through the array for your insert query?

How to give a unique url/id to a question posted by the user?

There's a form called discussion.php, in which a user will fill out his/her question/discussion and post it to savedisc.php. Some of the savedisc.php looks like this:
$message = $_POST['message'];
$title = $_POST['title'];
$represents = $_POST['represents'];
//connect to database
//save the content of discussion/question into the database for future use
$sql="INSERT INTO Discussion (Message, Title, Type)
VALUES
('$message','$title','$represents')";
//Display user's question/discussion again
echo $message . "<br />";
echo $title . "<br />";
echo $represents . "<br />";
It is not shown above, but I am saving the id field manually, i.e. via phpmyadmin as a auto increment and primary key of course. Therefore, all of the values in the table Discussion will have their own unique id. Once the question/discussion is saved, I want to be able to display $title of each question on wb.php as a link, which as of now looks like this(some code from wb.php):
$result = mysql_query("SELECT * FROM Discussion ORDER BY id DESC");
//When user clicks the question/discussion Title, he/she will be directed to wbcomm.php
while($row = mysql_fetch_array($result))
{
echo "<a href='wbcomm.php' >{$row['Title']}</a><br />";
}
Until here, everything is working smooth. However, from here on, what I'm trying to do is, when the user clicks the question/discussion title via above code, I want him/her to be directed to wbcomm.php?id=1, where id=1 represents the unique id of the question/discussion. Some of the code from wbcomm.php is below:
if (isset($_GET['id']))
{
//connect to db
$wbid = mysql_real_escape_string($_GET['id']);
$sql = "SELECT * FROM Discussion WHERE id = '$wbid' LIMIT 1";
$res = mysql_query($sql);
if (mysql_num_rows() > 0) {
$discussion = mysql_fetch_object($res);
//display member's question here:
echo $discussion['id'] . "<br />";
echo $discussion['Title'] . "<br />";
echo $discussion['Type'] . "<br />";
echo $discussion['Message'] . "<br />";
}
else {
// discussion does not exist with ID
}
}
However, for some reason, the result is blank. I.e. the question/discussion doesn't even show up. What am I doing wrong? Is my procedure even correct?
Thank you.
In your wb.php, you create a link to wbcomm.php but you are not passing the ID of the discussion, so your $wbid will be empty. You need to pass the ID along with the link, like this:
while($row = mysql_fetch_array($result))
{
echo "<a href='wbcomm.php?id={$row['id']}' >{$row['Title']}</a><br />";
}
Your ID column is an autoincrement int type so you do not need to put it in quotes or escape it. You should definitely test it to see if it's numeric, though.
Use this SQL mysql_num_rows($res) > 0

print passes values to Mysql

i am using this code to add values to database
<?php
$debdes = $_POST['debdes'];
$debamt = $_POST['debamt'];
$crdes = $_POST['crdes'];
$cramt = $_POST['cramt'];
$date = $_POST['date'];
include_once ("db.php");
$ucbook = "INSERT INTO cbook(debdes,debamt,crdes,cramt,date) VALUES ('$debdes','$debamt','$crdes','$cramt','$date');";
if (mysql_query($ucbook))
echo "One Record Updated Successfully with the following details <br/>";
else
echo mysql_error();
?>
now i want that when query pass this show me that which values are added like this
"Following record is updated successfully
debamt = 1000
debdes = test
end "
if the query does not fails, you can just show your values:
.
.
.
if (mysql_query($ucbook)){
echo "One Record Updated Successfully with the following details <br/>";
echo "debdes=$debdes <br>" ;
echo "debamt=$debamt <br>" ;
echo "crdes=$crdes <br>" ;
echo "cramt=$cramt <br>" ;
echo "date=$date <br>" ;
echo "end";
}
else
{
echo "Error while inserting data :".mysql_error()."<br/>";
}
.
.
.
there are two ways to do it:
if you one of the fields that you are inserting is a primary key, then search the table for the record you updated, and then display the resultant data.
If your primary key is generated automatically using auto_increment, then use mysql_insert_id.
Cheers,
jrh

Categories