gpt4 book ai didi

javascript - 如何在 php 文件的 SQL 查询中使用下拉列表的选定选项值?

转载 作者:行者123 更新时间:2023-11-29 18:54:10 25 4
gpt4 key购买 nike

我创建了一个带有下拉列表和上传文件的表单。下拉列表值是从数据库填充的。此表单的操作文件是 add.php 文件,它更新数据库并将文件上传到数据库中。我想从用户从下拉列表中选择的选项更新数据库,该下拉列表的选择名称为“to_user”,并且对于数据库中的该行,将上传与所选选项值相对应的文件。我在 add.php 文件中使用此 SQL 查询:

 mysql_query(" UPDATE TABLE2 SET COL6=('$filename') WHERE COL1=$_POST['to_user'] ");

我收到错误。我应该怎么办?错误 :解析错误:语法错误,意外的 '' (T_ENCAPSED_AND_WHITESPACE),第 21 行/Applications/XAMPP/xamppfiles/htdocs/add.php 中需要标识符 (T_STRING) 或变量 (T_VARIABLE) 或数字 (T_NUM_STRING)

index.php - webpage which has the drop down list and file uploading option

add.php- action file of the form created in index.php file

index.php

<!DOCTYPE html>
<html>
<head>
<title>Sch </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1”>
<link rel="stylesheet" href="https://www.w3schools.com/tags/tag_select.asp">
<link rel = "stylesheet" href="custom.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

<style>


div.container {
margin: 20px 0 20px 0;
padding: 20px;
}
div.space {
margin: 2px 0 2px 0;
padding: 1px;
}
div.space1 {
margin: 2px 0 2px 0;
padding: 1px;
}
div.space2 {
margin: 2px 0 2px 0;
padding: 2px;
}

div.space3 {
margin: 2px 0 2px 0;
padding: 2px;
}

div.space4 {
margin: 2px 0 2px 0;
padding: 2px;
}

div.space5 {
margin: 10px 0 15px 0;
padding: 2px;
}


</style>
</head>
<body>


<div class="container">
<h2>Select the Radioactive source and upload the documents:</h2>
</div>


<form enctype="multipart/form-data" action="add.php" method="POST">

<div class="space">
<?php include('d2.php') ?>

<select name="to_user" class="form-control">
<option value="pick">Radioactive source</option>
<?php
$sql = mysqli_query($con, "SELECT DISTINCT COL1 FROM TABLE2");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql))
{
echo "<option value='". $row['COL1'] ."'>" .$row['COL1'] ."</option>" ;
}
?>
</select>
</div>

<div class="space1">
<p>
Upload NOC file :<br>
<input type="file" name="datafile1" size="40">
</p> </div>

<div class="space2">
<p>
Upload LT file :<br>
<input type="file" name="datafile2" size="40">
</p> </div>


<div class="space3">
<p>
Upload Import Noc file :<br>
<input type="file" name="datafile3" size="40">
</p> </div>

<div class="space4">
<p>
Upload Photo Inventory file :<br>
<input type="file" name="datafile4" size="40">
</p>
</div>

<div class="space5">
<input type="submit" value=Submit
</div>
</form>

<div class="background">
<div class="transbox">

</body>
<footer>done </footer> </html>

添加.php

<?php include('index2.php') ?> 
<?php

$file1=( $_FILES['datafile1']['name']);
$file_size1 = $_FILES['datafile1']['size'];

$file2=( $_FILES['datafile2']['name']);
$file_size2 = $_FILES['datafile2']['size'];

$file3=( $_FILES['datafile3']['name']);
$file_size3 = $_FILES['datafile3']['size'];

$file4=( $_FILES['datafile4']['name']);
$file_size4 = $_FILES['datafile4']['size'];

mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("CSV_DB") or die(mysql_error()) ;

if($file_size1 >0)
{
mysql_query("UPDATE Table2 SET COL6=('$file1') WHERE COL1=$_POST['to_user']")or die('Error, query failed');
}
if($file_size2 >0)
{
mysql_query("UPDATE Table2 SET COL7=('$file2') WHERE COL1=$_POST['to_user']")or die('Error, query failed');
}
if($file_size3 >0)
{
mysql_query("UPDATE Table2 SET COL8=('$file3') WHERE COL1=$_POST['to_user']")or die('Error, query failed');
}
if($file_size4 >0)
{
mysql_query("UPDATE Table2 SET COL9=('$file4') WHERE COL1=$_POST['to_user']")or die('Error, query failed');
}

echo "<br>File $file1 uploaded<br>";
echo "<br>File $file2 uploaded<br>";
echo "<br>File $file3 uploaded<br>";
echo "<br>File $file4 uploaded<br>";

?>

最佳答案

尝试:

mysql_query(" UPDATE TABLE2 SET COL6=('$filename') WHERE COL1=" . $_POST['to_user'] . " ")

PHP 在字符串内包含数组时会出现问题,就像添加 $filename 一样。

根据变量类型,您可能需要向查询添加引号:

mysql_query(" UPDATE TABLE2 SET COL6=('$filename') WHERE COL1='" . $_POST['to_user'] . "' ")

关于javascript - 如何在 php 文件的 SQL 查询中使用下拉列表的选定选项值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44228504/

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