gpt4 book ai didi

php - 使用下拉菜单从表单插入表格

转载 作者:行者123 更新时间:2023-11-30 00:09:03 26 4
gpt4 key购买 nike

我正在尝试在表中插入订单,但我的 sql 未正确执行,我已经一遍又一遍地阅读此内容,但找不到我错过的地方。我认为这可能是插入错误,而且我认为因为我的两个字段是其他表的外键,我是否做错了?或者是用 php 吗?你能查看我的代码吗?

这里是:

<?PHP
session_start();
$host="xxxxx"; // Host name
$username="xxxx"; // Mysql username
$password="xxxx"; // Mysql password
$db_name="xxxx"; // Database name
$tbl_name="uxxxx";


// 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

?>



<!DOCTYPE html>
<html lang="en">
<head>
<title>Formulario De Encomenda</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<link type="text/css" rel="stylesheet" href="css/stylesEditF.css"></link>
<link type="text/css" rel="stylesheet" href="css/stylesEditF.php"></link>
</head>
<body>

<form action="" method="post">
<input type ="hidden" name="idEncomenda">
<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">
<input type="hidden" name="IdEncomenda" >
</p>
<p>
<label>Escolher Produto</label>
<select id="produtos" >
<?php

$query = $link->query("SELECT * FROM produto");
while ( $row = $query->fetch_array() ) {
echo "<option data-code=". $row['codigoDeBarras'] ." data-price=". $row['Preco'] .">" . $row['nome'] . "</option>";
}
?>
<input type="hidden" name="Produto" value="1">
</select>
</p>
<p>
<label>Escolher Fornecedor</label>
<select id="fornecedor">
<?php
$query = $link->query("SELECT * FROM fornecedor");
while ( $row = $query->fetch_array() ) {
echo "<option data-nif=". $row['NIF'] .">". $row['Nome'] ."</option>";

}
?>
<input type="hidden" name="Fornecedor" >
</select>
</p>
<p>



<label>Quantidade</label>
<input type="number" min="1" name="Quantidade" >
</p>
<p>
<label>Preco(Unidade)</label>
<span id="price">0.00</span>
<input type="hidden" name="preco" >
</p>
<p>
<label>Valor Total</label>
<span id="total_price">0.00</span>
<input type="hidden" name="valor" >
</p>
<input type="submit" name="Enviar" id="Enviar" value="Enviar" />
</form>

<?php

if( $_POST != '' && $_POST != NULL ){

$encomenda_data = $_POST['DataDeEncomenda'];
$encomenda_qty = $_POST['Quantidade'];
$encomenda_nif = $_POST['Fornecedor'];
$encomenda_codigo = $_POST['Produto'];
$encomenda_preco = $_POST['preco'];
$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");
header('Location: paginaEncomendaIn.php');

}





?>
<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>

</body>
</html>


<?php $link->close(); ?>

最佳答案

echo $sql3 并查看实际查询。这将使您了解您的查询出了什么问题。并更改下面的行$result = mysqli_query($link, $sql3) 或 die("无法插入");到$result = mysqli_query($link, $sql3) 或 die("无法插入".mysqli_error());

这将打印导致您的 sql 无法正确执行的错误,以便您知道您的 sql 有问题。

我在你的代码中注意到的另一件事是你使用 header('位置:paginaEncomendaIn.php');

Php 会抛出 Cannot edit header information...因为你已经在上面启动了 html 代码。将您在表单下方编写的 php 代码移至您启动 HTML 的上方行。

关于php - 使用下拉菜单从表单插入表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24236027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com