所以我想創建一個用戶界面,他們可以通過PHP UX插入,刪除和更新他們在SQL數據庫中的表。我有基本的工作,但我碰到了一堵牆,試圖保持一種動態的感覺。因此,讓我們假設,一個新的列被添加到他們試圖從事物的sql方面更新的特定表中。現在我已經知道了它會顯示添加的新列,但是如何在UPDATE sql查詢中包含新列。我已經爲每一列指定了變量(即$ user,$ pass,$ email),但是我的問題是如果他們添加了一個新列,如何設置代碼以便自動添加一個新變量並保持動態?我瘋了,真的需要別人的幫助,因爲聽起來很簡單。 (是的,我對此有所瞭解)。更新表使用PHP和SQL服務器(DYNAMICALLY!)
謝謝你的幫助!請讓我知道如何爲任何人澄清這個問題。
下面是我的工作代碼,用於更新固定列的表格。請告知我需要添加或更改的內容。
<?php
$result = sqlsrv_query($con,"SELECT * FROM table");
$colresult = sqlsrv_query($con,"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table' AND TABLE_SCHEMA='schema'");
$serverName = xxx";
$connectionInfo = array("Database"=>"db", "UID"=>"user", "PWD"=>"pass", 'ReturnDatesAsStrings'=> true, "CharacterSet" => 'utf-8');
$con = sqlsrv_connect($serverName, $connectionInfo);
$numfields = sqlsrv_num_fields($result);
if($con) {
echo "Connection established.<br />";
}
else{
echo "Connection could not be established.<br />";
die(print_r(sqlsrv_errors(), true));
}
if (isset($_POST['updatemarked']))
{
foreach($_POST["id"] AS $id){
**$user = $_POST["username"][$id];
$pass = $_POST["password"][$id];
$email = $_POST["email"][$id];**
if(!$user){
}
else{
$sqlupdate= sqlsrv_query($con,"UPDATE table SET username = '$user' WHERE id = $id");
}
if(!$pass){
//$pass = 'empty';//$row['password'];
}
else{
$sqlupdate= sqlsrv_query($con,"UPDATE table SET password = '$pass' WHERE id = $id");
}
if(!$email){
//$email = 'empty';//$row['email'];
}
else{
$sqlupdate= sqlsrv_query($con,"UPDATE table SET email = '$email' WHERE id = $id");
}
}
}
echo '
<form action="table.php" method="post">
<table table class="dynamicTable tableTools table table-striped table-bordered table- condensed table-white">';
echo '<tr>';
echo '<th width="50" align="center"><span style=color:black><strong>Select(s)</strong> </span></th>';
while($row = sqlsrv_fetch($colresult)){
$fieldname = sqlsrv_get_field($colresult, $i);
echo "<th>".$fieldname."</th>";
}
echo "</tr>";
while ($row = sqlsrv_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" name="id[]" value="'.$row['id'].'" /></td>';
echo "<td>".$row['id']."</td>";
echo '<td><input type="text" name="username['.$row['id'].']" placeholder="'.$row['username'].'"/></td>';
echo '<td><input type="text" name="password['.$row['id'].']" placeholder="'.$row['password'].'"/></td>';
echo '<td><input type="text" name="email['.$row['id'].']" placeholder="'.$row['email'].'"/></td>';
echo "</tr>";
}
echo'</table>';
echo'<br />';
echo '<div align="center">';
echo '<input type="submit" name="updatemarked" value="Update Selected" />';
echo '</div>';
echo '</form>';
?>
我注意到錯誤@此行 - > $ serverName = xxx「; – Ashish
忽略了,我用了一個虛擬servername –
你爲什麼喊? –