[英]Insert into table from form with dropdown
I'm trying to insert orders in my table but my sql is not executing correctly, i have read this over and over again and i can't find where did i miss. 我试图在我的表中插入订单,但是我的sql无法正确执行,我已经读了一遍又一遍,我找不到我想念的地方。 I think it might be the insert into that maybe wrong, and i think that because 2 of my fields are foregin keys to other tables, is it there that i'm doing wrong ?
我认为这可能是错误插入的插入,并且我认为因为我的两个字段都是其他表的前向键,所以我做错了吗? or is it with the php ?
还是与PHP? Can you view my code ?
您可以查看我的代码吗?
Here it is : 这里是 :
<?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 and view the actual query. 回显$ sql3并查看实际查询。 That will give you idea of what is wrong with your query.
这将使您了解查询的问题所在。 and change the line below $result = mysqli_query($link, $sql3) or die("Could not insert");
并更改下面的行$ result = mysqli_query($ link,$ sql3)或die(“无法插入”); to $result = mysqli_query($link, $sql3) or die("Could not insert".mysqli_error());
到$ result = mysqli_query($ link,$ sql3)或die(“无法插入” .mysqli_error());
That will print error that is causing your sql to not execute properly so you know that is wrong with your sql. 这将打印导致您的sql无法正确执行的错误,因此您知道这是您的sql错误。
Another thing I noticed in your code is your use of header('Location: paginaEncomendaIn.php'); 我在您的代码中注意到的另一件事是您使用标头('Location:paginaEncomendaIn.php');
Php will throw Cannot modify header information....because you have html code already started above it . PHP将抛出无法修改标头信息。...因为您已经在其上面启动了html代码。 Move your php code that you have written below the form to the line above where you have started HTML.
将您在表单下方编写的php代码移动到启动HTML的上方行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.