所以,我正在做一个php / mysqli的项目,我正在尝试做一个销售系统,我有3个表涉及卖点。一个是我要传递产品的地方,另一个是我想通过卖家ID的地方 对于我需要传递fk(product_id)的产品,所以我可以得到价格和我的jquery过程它做数学。目前系统正在运行,但我不知道如何使用这个,所以我可以选择我的数据库产品和卖家而不是表格上的选项。我从未做过销售系统,所以我在这方面有点亏。
这是我的代码:
<script type="text/javascript">
jQuery(document).ready(function($){
var codigoDeBarras = $('input[name="codigoDeBarras"]');
var nif = $('input[name="NIF"]');
var valorUnidade = $('input[name="valorUnidade"]');
var valorTotal = $('input[name="valorTotal"]');
var quantidade = $('input[name="Quantidade"]');
$('select#produtos').change(function(){
if( !$(this).find('option:selected').val() == '' ){
var price = parseInt($(this).find('option:selected').attr('data-price'));
var code = $(this).find('option:selected').attr('data-code');
$('#price').html(price);
$('#total_price').html(price * parseInt(quantidade.val()));
codigoDeBarras.val(code);
valorUnidade.val(price);
valorTotal.val(price * parseInt(quantidade.val()));
}
});
quantidade.bind('click keyup', function(){
if( !$('select#produtos').find('option:selected').val() == '' ){
var price = $('select#produtos').find('option:selected').attr('data-price');
$('#total_price').html(price * parseInt($(this).val()));
valorTotal.val(price * parseInt(quantidade.val()));
}
});
$('select#fornecedor').change(function(){
if( !$(this).find('option:selected').val() == '' ){
nif.val($(this).find('option:selected').attr('data-nif'));
}
});
});
</script>
<?PHP
session_start();
$host="xxxx"; // Host name
$username="xxxx"; // Mysql username
$password="xxxx"; // Mysql password
$db_name="xxx"; // Database name
$tbl_name="utilizador";
// Connect to server and select databse.
$link = mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db($link , $db_name)or die("cannot select DB");
// CHECK LOG IN
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
echo "";
} else {
header('Location: ../index.php');
}
// END OF LOG IN CHECK
$link->close();
?>
<html lang="en">
<link type="text/css" rel="stylesheet" href="css/stylesVenda.css"></link>
<link type="text/css" rel="stylesheet" href="stylesF.php"></link>
</head>
<body>
<div id="background"> </div>
<div id="topBar">
<div class="topBox">
<i class="fa fa-bars"></i>
</div>
<div id="rightSideBox">
<h2>WineControl</h2>
</div>
<a href="homepage.php"><h2>Homepage</h2></a>
</div>
<div id="sideBar">
<div id="add">
</div>
</div>
<div id="headerWrap">
<div id="container">
<div class="moveDown">
</div>
<div class="container">
<form action="" method="post">
<input type="hidden" name="codigoDeBarras">
<input type="hidden" name="NIF">
<input type="hidden" name="valorUnidade">
<input type="hidden" name="valorTotal">
<p>
<label>Data</label>
<input type="date" name="DataDeEncomenda">
</p>
<p>
<label>Escolher Produto</label>
<select id="produtos">
<option value="">Escolher Produto</option>
<option data-price="5.00" data-code="109000">Produto 1</option>
<option data-price="10.00" data-code="109001">Produto 2</option>
<option data-price="15.00" data-code="109002">Produto 3</option>
</select>
</p>
<p>
<label>Escolher Fornecedor</label>
<select id="fornecedor">
<option value="">Escolher Fornecedor</option>
<option data-nif="900001" data-code="109000">Fornecedor 1</option>
</select>
</p>
<p>
<label>Quantidade</label>
<input type="number" min="1" name="Quantidade" value="1">
</p>
<p>
<label>Preco(Unidade)</label>
<span id="price">0.00</span>
</p>
<p>
<label>Valor Total</label>
<span id="total_price">0.00</span>
</p>
</form>
</div>
</div>
</div>
</body>
</html>
我知道这是一个很大的问题,但是如果你能给我一个如何理解它的想法,那将非常有帮助。
编辑:所以,现在我可以将jquery中的数据导入到数组中,但仍然不知道如何将所选选项应用到我的数据库中......这是我的代码: <?php
$mysqli = new mysqli("xxxxx", "xxxxx", "xxxx", "xxxx", xxxx);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if( $_POST != '' && $_POST != NULL ){
$query = "SELECT * FROM produto";
$from = 0;
$to = 30;
$query .= " LIMIT ".$from.",".$to;
$result = mysqli_query($link, $query) or die("SQL Error 1: " . $mysqli->error);
while ($row = $result->fetch_assoc()) {
$produtos[] = array(
'Preco' => $row['Preco'],
'Nome' => $row['nome']
);
}
echo json_encode($produtos);
$query2 = "SELECT * FROM fornecedor";
$from = 0;
$to = 30;
$query .= " LIMIT ".$from.",".$to;
$result = mysqli_query($link, $query2) or die("SQL Error 1: " . $mysqli->error);
while ($row = $result->fetch_assoc()) {
$fornecedor[] = array(
'NIF' => $row['NIF'],
'Nome' => $row['Nome']
);
}
echo json_encode($fornecedor);
print_r($fornecedor);
$encomenda_data = $_POST['DataDeEncomenda'];
$encomenda_qty = $_POST['Quantidade'];
$encomenda_nif = $fornecedor[0]; // Forget this line and the other bellow ( testing purpose)
$encomenda_codigo = $_POST['Produto'];
$encomenda_preco = $produtos[0];
$encomenda_total = $_POST['valor'];
$sql3 = "INSERT INTO encomendain ( DataDeEncomenda, Quantidade, NIF, codigoDeBarras, Preço, ValorTotal) VALUES ( '$encomenda_data' , '$encomenda_qty' , '$encomenda_nif' , '$encomenda_codigo' , '$encomenda_preco', '$encomenda_total') ";
$result = mysqli_query($link, $sql3) or die("Could not insert".mysqli_error($link));
header('Location: paginaEncomendaIn.php');
}
?>
答案 0 :(得分:-1)
连接到您的数据库使用(从http://www.php.net/manual/en/mysqli.quickstart.connections.php复制)
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
从表中读取并存储在主要从http://www.jqwidgets.com/jquery-widgets-documentation/documentation/phpintegration/bind-jquery-grid-to-mysql-database-using-php.htm复制的jquery数组中,以适应使用的mysqli
// get data and store in a json array
$query = "SELECT * FROM customers";
$from = 0;
$to = 30;
$query .= " LIMIT ".$from.",".$to;
$result = mysqli_query($query) or die("SQL Error 1: " . $mysqli->error);
while ($row = $res->fetch_assoc()) {
$customers[] = array(
'CompanyName' => $row['CompanyName'],
'ContactName' => $row['ContactName'],
'ContactTitle' => $row['ContactTitle'],
'Address' => $row['Address'],
'City' => $row['City']
);
}
echo json_encode($customers);
您需要修改此项以从数据库中读取并填充html表单中的字段。