我在数据库中有44个列的表,如col1,col2,col3 .... col44这个值可能会有所不同 我想将数据插入到该表中而不是我们拥有的所有列,仅针对该表中的某些列,这些列在运行时由用户选择,并为这些列提供值,这些值应放入单行的相应列中用户选择其他列的内容在该行中应为空。
请帮助您了解如何为此情况编写代码。
<?php
include("con_gen.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.5/js/bootstrap-select.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.5/css/bootstrap-select.min.css">
</head>
<style type="text/css">
.form-horizontal{
margin: 40px;
}
</style>
<body>
<form class="form-horizontal" role="form" method="post" action="" >
<h3> Enter Details</h3>
<?php
$link = mysqli_connect("localhost","root","") or die("failed to connect to server !!");
mysqli_select_db($link,"idcard");
$result = mysqli_query($link,"SELECT value FROM combo1");
$num_rows = mysqli_num_rows($result);
echo "$num_rows Rows\n";
if ($result->num_rows > 0) {
// output data of each row
$array = Array();
$array1 = Array();
while($row = $result->fetch_assoc()) {
//echo "<br> value: ". $row['value']. "<br>";
$array[] = $row['value'];
# code...
# code...}
}
print_r($array);
//echo "fffffff";
$sql = "SELECT static_name FROM static_values";
$result = mysqli_query($link, $sql);
if ($result && mysqli_num_rows($result) > 0)
while($row = mysqli_fetch_array($result)){
//echo "<option>" . $row['static_name'] . "</option>";
$array1[]=$row['static_name'];
//echo $source;
}
print_r($array1);
foreach ($array as $row)
{
if(in_array($row, $array1))
{
$source=Array();
$sql = "SELECT source_table,Alias_name FROM static_values where static_name='$row'";
$result = mysqli_query($link, $sql);
if ($result && mysqli_num_rows($result) > 0)
while($row = mysqli_fetch_array($result)){
//echo "<option>" . $row['static_name'] . "</option>";
$source=$row['source_table'];
$source1=$row['Alias_name'];
//echo $source;
}
//print_r($source1);
?>
<div class="row-fluid">
<div class="span3 bgcolor">
<?php echo "<label>".$source1."</label>";?>
<select id="static" name="static" data-live-search="true" class="selectpicker form-control" >
<!--<select name="customer_type" id="customer_type" class="span12"> <option>Type 1</option> <option>Type 2</option> </select> -->
<?php echo "<option>".$row. "</option>";
foreach ( $Data->$source as $key =>$item) echo "<option value=".$key.">".$item."</option>"; ?>
</select>
</div>
</div>
<?php
}
else
{
?>
<div class="row-fluid">
<div class="span3 bgcolor">
<?php echo "<label>" . $row . "</label>";?>
<input id="ggg" type="text" placeholder=" Enter Value " name="text_value" style="width:100%" class="form-control" />
</select>
</div>
</div>
<?php
# code...
}
}
}
# code...
?>
<br>
<input type="submit" value="SUBMIT" class="btn btn-success" name="submit" />
<?php
?>
</form>
</body>
</html>
<?php
//include('db.php');
$link = mysqli_connect("localhost","root","") or die("failed to connect to server !!");
mysqli_select_db($link,"idcard");
//$arr=Array();
$sql1 = "SELECT * FROM combo1";
$result = mysqli_query($link, $sql1);
if ($result && mysqli_num_rows($result) > 0)
echo "ddd";
while($row = mysqli_fetch_array($result)){
//echo "<option>" . $row['static_name'] . "</option>";
$arr[]=$row['column'];
// print_r($arr);
}
print_r($arr);
if(isset($_REQUEST['submit']))
{
$errorMessage = "";
if ($errorMessage != "" ) {
echo "<p class='message'>" .$errorMessage. "</p>" ;
}
//$ins="insert into `column_list` value($row)";
$sql = array();
foreach( $array as $row ) {
$sql[] = '("'.mysql_real_escape_string($row['static']).'", '.$row['text_value'].')';
}
mysql_query('INSERT INTO `column_list` (static,text_value) VALUES '.implode(',', $sql));
}
$test=mysqli_query($link,$sql) or die(mysqli_error($link));
if($test)
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
else
echo "<script type='text/javascript'>alert('failed!')</script>";
exit();
?>
答案 0 :(得分:0)
我会提供一些关于如何处理这个问题的提示。
首先,您的表定义必须允许用户可以选择忽略的列中的NULL
值。
其次,如果您知道需要插入col3
和col5
值,而不是其他人,那么这样的INSERT
查询就可以解决问题
INSERT INTO table (col3, col5) VALUES (1, 2);
或者,您可以使用这样的查询
INSERT INTO table (col1, col2, col3, col4, col5, ... coll44)
VALUES (NULL, NULL, NULL, 1, NULL, 2, NULL,... NULL)
我在该示例中使用了...
来避免写出所有列的名称。
观察:44列是一张桌子的很多。当表稀疏时(当不使用所有列时),情况会更糟。您可能希望使用某种规范化的键/值存储来调查。你可以读到这些东西。
在处理稀疏行(其中许多值为null,并且每行中的空值不同)时在SQL中遇到的问题之一是您生成{{1}的绝对要求每种情况动态的语句。你可以用php做到这一点。或者,也许你可以使用存储过程来完成它。但遗憾的是,你无法避免这样做。