At the moment this code updates the user info in MYSQLTABLE1. What I also want done is user info to be copied to WMYSQLTABLE which I can do but I also want a code from MYSQLTABLE2 to be copied over into a column in WMYSQLTABLE as well. Here is the part I need changing:
$sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
('','$ip','$date','$address')";
$res_insert2 = mysql_query($sql_insert2)or die(mysql_error());
//Need to also insert code from first row from column named 'codes' in MYSQLTABLE2.
Actual code, it's a bit messy at the moment and the if/else statements do the exact same at the moment. It works but I get the error " Column count doesn't match value count at row 1" because I cannot fill the last column with the code from MYSQLTABLE2.
<?php
include("includes/config.php");
include("includes/mysql.php");
include("amount.php");
if(isset($_POST['func']))
{
$address = $_POST['address'];
$ip = $_POST['ip'];
$date = $_POST['date'];
$time = $_POST['time'];
$price = $_POST['price'];
$increment = $_POST['increment'];
$id = $_POST['id'];
$num = 0;
$sql_check_address = "SELECT * FROM ".MYSQLTABLE1." WHERE address='$address'";
$res_check_address = mysql_query($sql_check_address)or die(mysql_error());
$num = mysql_num_rows($res_check_address);
$row = mysql_fetch_assoc($res_check_address);
if($num > 0)
{
$address = $row['address'];
$ip = $row['ip'];
$date = $row['date'];
$oldprice = $row['price'];
$id = $row['id'];
$newprice = $oldprice - $payAmount*200;
$newinc = $increment - 200;
$sql_update1 = "UPDATE ".MYSQLTABLE1." SET ip='$ip',date='$date',price='$newprice',increment='$newinc',address='$address' WHERE id='$id'";
$res_update1 = mysql_query($sql_update1)or die(mysql_error());
/////////////////Insert user info and copy code from MYSQLTABLE2 to WMYSQLTABLE2
$sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
('','$ip','$date','$address')";
$res_insert2 = mysql_query($sql_insert2)or die(mysql_error());
}
else{
$address = $row['address'];
$ip = $row['ip'];
$date = $row['date'];
$oldprice = $row['price'];
$id = $row['id'];
$newprice = $oldprice - $payAmount*200;
$newinc = $increment - 200;
$sql_update = "UPDATE ".MYSQLTABLE1." SET ip='$ip',date='$date',price='$newprice',increment='$newinc',address='$address' WHERE id='$id'";
$res_update = mysql_query($sql_update)or die(mysql_error());
/////////////////Insert user info and copy code from MYSQLTABLE2 to WMYSQLTABLE2
$sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
('','$ip','$date','$address')";
$res_insert2 = mysql_query($sql_insert2)or die(mysql_error());
e
}
}
Any help will be greatly appreciated.
The general form of the query would be:
$sql_insert = "INSERT INTO " . WMYSQLTABLE2 . "(code, ip, date, address)
SELECT code, '$ip', '$date', '$address'
FROM OtherTable
WHERE <put something here to select the row>";
Related
I have a 200 inputs ( 200 rows ) in my web page , the back end user some time enter 20 or 50 inputs and left the rest empty
how to prevent empty inputs from insertion to db
I get around that by deleting the rows with condition but is consuming time
this is my code
<?php
include("db.php");
include("header.php");
if (isset($_POST['invoice_btn'])) {
$userId = $_POST['userId'];
$invoice_to = $_POST['companyName'];
$subTotal = $_POST['subTotal'];
$taxAmount = $_POST['taxAmount'];
$taxRate = $_POST['taxRate'];
$totalAftertax = $_POST['totalAftertax'];
$amountPaid = $_POST['amountPaid'];
$amountDue = $_POST['amountDue'];
$notes = $_POST['notes'];
$productCode = $_POST['productCode'];
$productName = $_POST['productName'];
$quantity = $_POST['quantity'];
$price = $_POST['price'];
$total = $_POST['total'];
$dateTime = $_POST['dateTime'];
$submitbutton = $_POST['invoice_btn'];
if ($submitbutton) {
if (empty($productCode)) {
die(" Product code empty ");
} else {
$sqlInsert = "INSERT INTO invoice_order (invoice_to,order_date, order_total_before_tax, order_total_tax, order_tax_per, order_total_after_tax, order_amount_paid, order_total_amount_due, notes)
VALUES ('$invoice_to' ,'$dateTime', '$subTotal', '$taxAmount','$taxRate', '$totalAftertax','$amountPaid', '$amountDue','$notes')";
$result = mysqli_query($conn, $sqlInsert);
// The mysqli_insert_id() function returns the id (generated with AUTO_INCREMENT) from the last query.
$lastInsertId = mysqli_insert_id($conn);
foreach ($productCode as $index => $productCodes) {
$s_productCode = $productCodes;
$s_productName = $productName[$index];
$s_quantity = $quantity[$index];
$s_price = $price[$index];
$s_total = $total[$index];
$sqlInsertItem = "INSERT INTO invoice_order_item (order_id, item_code, item_name, order_item_quantity, order_item_price, order_item_final_amount)
VALUES ( '$lastInsertId' , '$s_productCode' , '$s_productName' , '$s_quantity', '$s_price', '$s_total')";
$result2 = mysqli_query($conn, $sqlInsertItem);
// Update Quantity on Hand from Produc table
$sqlUpdateQty = "UPDATE product SET pro_quantity = pro_quantity-$s_quantity WHERE pro_id = $s_productCode ";
$result3 = mysqli_query($conn, $sqlUpdateQty);
//delete extra rows that are empty .
$sqlDelete = "DELETE FROM `invoice_order_item` WHERE `item_code`=''";
$Delresult = mysqli_query($conn, $sqlDelete);
} // end foreach
}; // end else
} // end if
In your foreach loop, add a condition before the insertion:
if (empty($s_productCode)) continue;
If the condition becomes true, continue will make the parser skip the rest of the code in the loop.
I want to calculate the bond balance using amount saved in the database and bond amount inputted on a form then insert the result to the database plus the other information. am using php and mysql.
below is the code I have
if (isset($_POST['submit'])) {
$ref_no = mysqli_real_escape_string($dbc, $_POST['ref_no']);
$cargo_des = mysqli_real_escape_string($dbc, $_POST['cargo_des']);
$te_no = mysqli_real_escape_string($dbc, $_POST['te_no']);
$bond = mysqli_real_escape_string($dbc, $_POST['bond']);
$bond_no = mysqli_real_escape_string($dbc, $_POST['bond_no']);
$bond_amount = mysqli_real_escape_string($dbc, $_POST['bond_amount']);
$date_lodged = mysqli_real_escape_string($dbc, $_POST['date_lodged']);
//$bond_balance = mysqli_real_escape_string($dbc, $_POST['bond_balance']);
$query = mysqli_query ($dbc, "SELECT amount FROM bond");
while( $row = mysqli_fetch_array($query) ) {
echo $row['amount'];
}
$bond_balance = 0;
$examout = $row['amount'];
$amount = $_POST['bond_amount'];
$bond_balance = $examout - $amount;
$q = "INSERT INTO new_bond (ref_no, cargo_des, te_no, bond, bond_no, bond_amount, date_lodged) "
. "VALUES ('$ref_no','$cargo_des','$te_no','$bond','$bond_no','$bond_amount','$date_lodged','$bond_balance')";
$r = mysqli_query($dbc, $q) or die(mysqli_error($dbc));
if ($r) {
echo '<script>alert("Success")</script>';
echo "<script>window.open('?page=new_bond','_self')</script>";
}
exit();
}
I have a confusion regarding table bond. How many records are there in bond table. I am writing down the sql query for taking the first encountered amount from bond table and doing the calculations.
"INSERT INTO new_bond (ref_no, cargo_des, te_no, bond, bond_no, bond_amount, date_lodged) "
. "VALUES ('$ref_no','$cargo_des','$te_no','$bond','$bond_no','$bond_amount','$date_lodged','((SELECT amount FROM bond LIMIT 1) - $amount)')";
I have tried this creating sample tables with minimal data. But let me know if this doesn't work.
this worked
$query = mysqli_query ($dbc, "SELECT amount FROM bond");
while( $row = mysqli_fetch_array($query) ){
$bond_balance = 0;
$examout = $row['amount'];
$amount = $_POST['bond_amount'];
$bond_balance = $examout - $amount;
}
I am trying to only allow a submission via the form only if a party_id exists in a table using empty, here is my code at the moment it is still allowing everything through even if there is no party_id.
Any help would be great.
if($_SERVER["REQUEST_METHOD"]== "POST") {
$party_id = (int)$_POST["partyid"];
$name = $_POST["name"];
$date = $_POST["date"];
$length = (int)$_POST["length"];
$sql = "SELECT * FROM `party` WHERE `party_id`='" . $party_id . "'";
$res = mysqli_query($link, $sql);
if(empty($party_id)) { #Were any records found?
print '<p>No Parties with that ID found! please press the back button to select another party</p>';
} else {
$record = mysqli_fetch_assoc($res);
$party_name = $record["party_name"];
$price = $record["price"];
$cost = $price * $length;
$bookable = true;
$sql2 = "SELECT * FROM `reservations`" or die("Unable to connect to database");
A simpler way might be to just check if the query returned any results like this.
if($_SERVER["REQUEST_METHOD"]== "POST") {
$party_id = (int)$_POST["partyid"];
$name = $_POST["name"];
$date = $_POST["date"];
$length = (int)$_POST["length"];
$sql = "SELECT * FROM `party` WHERE `party_id`='$party_id'";
$res = mysqli_query($link, $sql);
if ( mysqli_num_rows($res ) == 0 ) {
print '<p>No Parties with that ID found! please press the back button to select another party</p>';
} else {
$record = mysqli_fetch_assoc($res);
$party_name = $record["party_name"];
$price = $record["price"];
$cost = $price * $length;
$bookable = true;
$sql2 = "SELECT * FROM `reservations`" or die("Unable to connect to database");
Hi i am having an issue selecting a value form my table into a variable in the PHP so that I can calculate the cost of something
here is the code I have so far I want to be able to select a "cost" value from the table C_price where the values of I_type and a_type match
E.g. the table structure looks like this
ID=1,A_type=line,I_type=Head,cost=5
if on the form i enter line and head
i need to be able to get the value 5 in to a venerable i can use in calculations and insert into another table AKA i need to get cost into a variable somehow
the following was my try and i need help im new at all this so please help
$E_C;
$T_cost = "1";
$date = date("d.m.y");
$name = $_POST["from"];
$email = $_POST["email"];
$ref = $_POST["link"];
$i_type = $_POST["i_type"];
$a_type = $_POST["a_type"];
$extra = $_POST["extra"];
$des = $_POST["description"];
$BG = $_POST["BG"];
$bg_type = $_POST["BGtype"];
$msg = $_POST["message"];
$auto_reply = ("thanks for the email we will get back to you as soon as we can about the cost and how you can pay");
$msg = wordwrap($msg, 70);
$host = "localhost";// hostname
$USER = "root";// username
$PASS = "Password";// password
$DBNAME = "andrea";// databace name
$tbl_name = "c_price";// table name
$con = mysqli_connect("localhost", $USER, $PASS, $DBNAME)or die("mySQL server connection failed");
$all = "SELECT cost FROM C_price WHERE a_type=$a_type,i_type=$i_type";
$result = mysqli_query($con,$all) or die("Error getting total storse");
while($row = mysqli_fetch_array($result))
{
echo $row['cost'];
}
if ($a_type = 'waist' && $extra='Y')
{
$E_C = $cost * .3;
}
elseif ($a_type = 'knee' && $extra='Y')
{
$E_C = $cost * .35;
}
elseif ($a_type ='full' && $extra='Y')
{
$E_C = $cost * .4;
}
else
{
$E_C = 0;
}
$T_cost = $cost + $E_C;
if ($BG = 'y')
{
$T_cost = $T_cost + 10;
}
You can't use mysqli and mysql at a same time.. Mysqli is a class... So first change that things...
while($row = mysqli_fetch_array($result))
{
echo $row['cost'];
}
$news1 = mysqli_result($result, 0); // 0 is the index of the field, not the row
echo $news1;
echo $cost;`
Query should be like this...
$all = "SELECT cost FROM C_price WHERE a_type='$a_type'and i_type='$i_type'";
You cant mix mysql and mysqli
change this line In the while loop and add for error mysqli_error
$news1 = mysql_result($result, 0);
$news1 = mysqli_result($result) or die(mysqli_error());
and your query is wrong as well and A_type is not same as A_type and same goes for I_type as well
$all = "SELECT cost FROM C_price WHERE a_type=$a_type,i_type=$i_type";
//Change it to
$all = "SELECT cost FROM C_price WHERE A_type='$a_type'and I_type='$i_type'";
//and A_type is not same as a_type and same goes for I_type as well
Here I am trying to add all the columns up from one table echo out the result.
I have a table with Points that have been recorded by user, every points gets added I would like to add it up and print it out as result for the users score.
I am getting an error where the SUM Query is run. and the result is 0.
Here is the PHP:
<?php
// see if the form has been completed
include_once("php_includes/check_login_status.php");
//include_once("php_includes/db_conx.php");
// Initialize any variables that the page might echo
$username = "";
$weight = "";
$weighthist = "";
$id = "";
if(isset($_GET["u"])){
$username = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
}
$sql = "SELECT users.*, weighthistory.* FROM users JOIN weighthistory USING(id)";
$user_query = mysqli_query($db_conx, $sql);
// check if the user exists in the database
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$id = $row ["id"];
$username = $row ["username"];
$weight = $row["weight"];
$weighthist = $row["weighthist"];
$point_hist = $row["point_hist"];
}
// this is to calculate points score
$calweight = $weight - $weighthist;
$points = $calweight * 10;
$result = mysql_query('SELECT SUM (point_hist) AS value_sum FROM points_history');
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
?>
THIS IS THE HTML:
<div><b>Point Hist: </b> <?php echo $sum; ?> </div>
<input id="point_hist" type="hidden" name="point_hist" value="<?php echo $sum; ?>" readonly/>
</html>
I managed to figure it out, thanks for the tips here is the final result of my query etc...
$res = mysqli_query($db_conx,'SELECT sum(point_hist) FROM points_history');
if (FALSE === $res) die("Select sum failed: ".mysqli_error);
$row = mysqli_fetch_row($res);
$sum = $row[0];