I'm trying to fetch couple of single data in my server database but this is throwing some errors. The incoming data is correct. The search function just don't get completed.
Here's the code:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
define('HOST','xxxxxxxxxxx');
define('USER','xxxxxxxxxxxx');
define('PASS','xxxxxxxxx');
define('DB','xxxxxxxxxx');
$con = mysqli_connect(HOST,USER,PASS,DB);
$post_id = $_POST['id'];
$buyer_mobile = $_POST['mobile'];
$buyer_name = $_POST['name'];
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysqli_query($con,$sql);
$owner_mobile = $row['mobile'];
$sql = "select name from user where mobile='$owner_mobile'";
$r = mysqli_query($con,$sql);
$owner_name = $row['name'];
$sql = "INSERT INTO flat_booking (post_id,owner_mobile,owner_name,buyer_mobile,buyer_name) VALUES ('$post_id','$owner_mobile','$owner_name','$buyer_mobile','$buyer_name')";
if(mysqli_query($con,$sql)){
echo "Success";
}
else{
echo "error";
}
mysqli_close($con);
}else{
echo 'error1';
}
What am I doing wrong here? Maybe this:
$owner_mobile = $row['mobile'];
Thanks in advance!
create table flatower and add mobile column
$post_id = 1;
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysql_query($con,$sql);
$row = mysql_fetch_array($res);
$owner_mobile = $row[0]['mobile'];
Your problem is this line:
$owner_mobile = $row['mobile'];
You have not created the $row variable. For this you would need to do something such as:
Do this first:
<?php
$row = array();
while ($result = mysqli_fetch_assoc($res))
{
$row[] = $result;
}
?>
This allows you to do this:
<?php
foreach ($row as $r)
{
var_dump($r); print "<br />"; // One row from the DB per var dump
}
?>
Related
I am new to php and am trying to return a json response in a particular structure. Here is what I have tried so far:
$response = array();
if ($con) {
$sql = "select * from admission_view";
$result = mysqli_query($con, $sql);
if ($result) {
$x = 0;
while ($row = mysqli_fetch_assoc($result)) {
$response[$x]['id'] = $row['id'];
$response[$x]['name'] = $row['name'];
$response[$x]['isActive'] = $row['isActive'];
$response[$x]['branchId'] = $row['branchId'];
$response[$x]['branch'] = $row['branch'];
$response[$x]['customerGroupId'] = $row['customerGroupId'];
$response[$x]['customerGroup'] = $row['customerGroup'];
$response[$x]['orderNo'] = $row['orderNo'];
$x++;
}
echo json_encode($response, JSON_PRETTY_PRINT);
}
} else {
echo "Connection error";
}
The code above returns this response:
However, instead of returning for example "branchId" and "branch" as individual properties, I want to pass their values inside a branchObject such that branch.id == "branchId" and branch.name == "branch".I mean, How may I return the response in the following structure:
And Here is how my database looks like:
How can I achieve this?
You ask for stuff that we are unsure if db result returns but as nice_dev pointed out, you need something like this:
$response = [];
if ($con) {
$sql = "select * from admission_view";
$result = mysqli_query($con, $sql);
if ($result) {
$x = 0;
while ($row = mysqli_fetch_assoc($result)) {
$response[$x]['id'] = $row['id'];
$response[$x]['name'] = $row['name'];
$response[$x]['isActive'] = $row['isActive'];
$response[$x]['branch']['id'] = $row['branchId'];
$response[$x]['branch']['name'] = $row['branch'];
$response[$x]['customerGroup']['id'] = $row['customerGroupId'];
$response[$x]['customerGroup']['name'] = $row['customerGroup'];
$response[$x]['customerGroup']['orderNo'] = $row['orderNo'];
$x++;
}
echo json_encode($response, JSON_PRETTY_PRINT);
}
} else {
echo "Connection error";
}
I would like to see where my code is incorrect. I want to store values from my database as a php array. Then I'd like to store the individual parts of the array as separate variables. Here is my code:
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = mysqli_fetch_row($result);
$comment0 = $comments[0];
$comment1 = $comments[1];
$comment2 = $comments[2];
$comment3 = $comments[3];
$comment4 = $comments[4];
$comment5 = $comments[5];
$comment6 = $comments[6];
$comment7 = $comments[7];
$comment8 = $comments[8];
$comment9 = $comments[9];
?>
This will run your mysql query and add each comment to an array of comments, then print the array.
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = array();
while($comment = mysqli_fetch_row($result)){
$comments[] = $comment;
}
print_r($comments);
?>
not sure if you are in console or through web server.
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = mysqli_fetch_row($result);
foreach($comments as $comment){
echo print_r($comment,1).'--------------\r\n<br>\r\n';
}
?>
this is called a loop. loop is your friend.
<?php
include 'dbh.php';
session_start();
if (isset($_GET['gmail'])) {
$gname = $_GET['gmail'];
}
$gname = mysqli_real_escape_string($connect, $gname);
$_SESSION['myusername'] = $gname;
$today = date("d.m.y");
$k=0;
$sql = "SELECT cart_fext FROM cart WHERE cart_sess = '$sess'";
$result=mysqli_query($connect, $sql);
$kode[$k] = array();
$kame[$k] = array();
$kesc[$k] = array();
$kail[$k] = array();
$kid[$k] = array();
$kate[$k] = array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
extract($row);
$k = $k + 1;
$cname=$row['cart_item_name'];
$csize=$row['item_size'];
$cdesc=$row['cart_desc'];
$cfpath=$row['cart_fpath'];
$cfext=$row['cart_fext'];
$ccode=$row['cart_itemcode'];
$cuserid=$row['cart_usrid'];
$kode[$k] = $ccode;
$kame[$k] = $cname;
$kesc[$k] = $cdesc;
$kail[$k] = $gname;
$kid[$k] = $cuserid;
$kate[$k] = $today;
}
for($i=1; $i<=$k; $i++) {
$sqlsal = "INSERT INTO sales (s_code, s_name, s_desc, s_mail, s_userid, s_date) VALUES ('$kode[$i]', '$kame[$i]', '$kesc[$i]', '$kail[$i]', '$kid[$i]' ,'$kate[$i]')";
$result=mysqli_query($connect, $sqlsal);
}
header("location:makedir.php");
?>
my table sales is just not accepting data, dbh.php is to connect to the database
I don't understand what is wrong with this script?
please help?
I have checked your insert query it is running fine but I think the problem is with your SELECT query
SELECT cart_fext FROM cart WHERE cart_sess = '$sess'
please print the result of the query. I think that it may not be giving any result.
How can I perform a autocompute in my database ex. the value of Stock and Quantity(Quantity-Stock) the answer will be save in CarryO column
create.php
<?php
require_once 'dbconfig.php';
$con = mysql_connect("localhost","root","");
if($con)
{
mysql_select_db("testproduct",$con);
}
if($_POST)
{
$sql = mysql_query("SELECT * FROM tblproduct WHERE id = '".$_POST['pid']."'");
$prod = mysql_fetch_array($sql);
$pname = $prod['name'];
$actualprice = $prod['actualprice'];
$sellprice = $prod['sellprice'];
$stock = $prod['Stock'];
$gname = $_POST['gname'];
$saledate = $_POST['saledate'];
$quantity = $_POST['quantity'];
$profit = $_POST['profit'];
$carryO = $_POST['carryO'];
$sells = $_POST['sells'];
$expense = $_POST['expense'];
try{
$stmt = $db_con->prepare("INSERT INTO tblsales(pname,gname,saledate,quantity,actualprice,sellprice,carryO,sells,expense,profit,stock)
VALUES(:upname,:ugname,:usaledate,:uquantity,:uactualprice,:usellprice,:ucarryO,:usells,:uexpense,:uprofit,:ustock)");
$stmt->bindParam(":upname", $pname);
$stmt->bindParam(":ugname", $gname);
$stmt->bindParam(":usaledate", $saledate);
$stmt->bindParam(":uquantity", $quantity);
$stmt->bindParam(":uactualprice", $actualprice);
$stmt->bindParam(":usellprice", $sellprice);
$stmt->bindParam(":ucarryO", $carryO);
$stmt->bindParam(":usells", $sells);
$stmt->bindParam(":uexpense", $expense);
$stmt->bindParam(":uprofit", $profit);
$stmt->bindParam(":ustock", $stock);
if($stmt->execute())
{
echo "Successfully Added";
}
else{
echo "Query Problem";
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}
?>
thanks for your help just new in php and please let me know if I can use your code or its only a example
Change this part:
$prod = mysql_query("SELECT * FROM tblproduct WHERE id = ".$_POST['pid']);
echo $prod;
$pname = [$prod['name']];
Into:
$sql = mysql_query("SELECT * FROM tblproduct WHERE id = '".$_POST['pid']."'");
$prod = mysql_fetch_array($sql);
$pname = $prod['name'];
You may want to try this.
$prod = mysql_query("SELECT * FROM tblproduct WHERE id = ".$_POST['pid'],$db_con); //$db_con must be your database connection
if(!$prod) { die("Database query failed: " . mysql_error()); } //always check if your query is properly done.
$pname = "";
while ($row = mysql_fetch_array($prod)) {
$pname = $row["name"]; }
also if you are fetching only one column which is the name then be specific to your query for fastest result. e.g. "SELECT name FROM tblproduct WHERE id = ".$_POST['pid']
I would appreciate it if anyone willing to tell how to echoing /print.
Below is the process of entering data into the database, before inserting it how can I echoing it to the table?
<?php
session_start();
if(isset($_POST['submit']))
{
include('class/stock_class.php');
$st = new st_exchange_conv(DEFAULT_SOURCE);
$from = mysql_real_escape_string(stripslashes($_POST['from']));
$value = floatval($_POST['amount']);
$date = date('Y-m-d H:i:s');
$_SESSION['selected'] = $from;
$stocks = $st->stocks();
asort($stocks);
foreach($stocks as $key=>$stock)
{
$st->convert($from,$key,$date);
$stc_price = $st->price($value);
$stock = mysql_real_escape_string(stripslashes($stock));
$count = "SELECT * FROM oc_stock WHERE stock = '$key'";
$result = mysql_query($count) or die(mysql_error());
$sql = '';
if(mysql_num_rows($result) == 1)
{
$sql = "UPDATE oc_stock SET stock_title = '$stock', stc_val = '$stc_price', date_updated = '$date' WHERE stock = '$key'";
}
else
{
$sql = "INSERT INTO oc_stock(stock_id,stock_title,stock,decimal_place,stc_val,date_updated) VALUES ('','$stock','$key','2',$stc_price,'$date')";
}
$result = mysql_query($sql) or die(mysql_error().'<br />'.$sql);
}
header("Location: index.php");
exit();
}
?>
Insert this:
echo "<table><tr><th>".implode(array_keys($stocks), '</th><th>')."</th></tr>";
foreach($stocks as $row) echo "<tr><td>".implode('</td><td>', $row)."</tr>";
echo "</table>";
Edit: If printing the data is the goal and the table-view is not important, I recommend print_r($stocks) instead.