2012-04-24 92 views
-1

我试图填充次表排除那些已经在客户端已预订的预约(如10:30 AM) 所以我有一个选择包括从上午9点到下午7点的时间以15分钟的步骤(9:45 am,10:00 am)上升。在PHP你怎么处理充满倍HTML选择框,在MySQL

<select name="times"> 
<option value="09:00">9:00am</option> 
..... 
<option value="19:00">7:00pm</option> 
</select> 

我需要一些如何删除一些已经存储在数据库中的时代

这会不会像?:

<?php 
$times = $_POST['times']; 
$dbc = mysqli_connect('localhost', 'root', '', 'salon') 
or die('error conneting to mysql'); 

$query = "SELECT times FROM `clients` WHERE `times` = '$times'"; 

$result = mysqli_query($dbc, $query); 
mysqli_close($dbc); 
?> 
<select> 
<?php 
$times = array('09:00','09:15', ..... '19:00'); 
foreach($times as $time); 
while($row = mysql_fetch_array($results)){ 
if($row = $time){ 
unset($time); 

echo '<option value="$time">$times</option>'; 
} 
else{ echo 'oh no'; 
} 
?> 
</select> 

以及类似的东西..反正我只是无法弄清楚。

在此先感谢所有帮助。

P.S我知道我的代码应该是更清洁,包括用strip_tags()mysql_real_escape_string()。 这只是提出一个问题

回答

0

的目的,这是一个完全的代码过早例子。你的代码中有很多错误。 BTW以下是正确的:

$times = $_POST['times'];

是一个数组,所以你不能用它像一个字符串。您可以使用逗号(,)分隔符破灭的阵列,因此您的变量$时间可能是这样的

$times_array = "in('09:00','09:30')";

下面是完整的例子

<?php 

    $times_post = $_POST['times']; 

    $times_array = "in('09:00','09:30')";// This will be your string after imploding post array 

    $dbc = mysqli_connect('localhost', 'root', '', 'shiv') or die('error conneting to mysql'); 

    $query = "SELECT times FROM `clients` WHERE `times` = $times_array"; 


    $results = mysqli_query($dbc, $query); 

    echo "<select>"; 

    $times = array('09:00','09:15','19:00'); 

    while($row = mysqli_fetch_array($results)){ 

     foreach($times as $key => $time){ 

      if($row[0] == $time){ 
       unset($times[$key]); 

      }else{ 
       echo 'oh no'; 
      } 
     } 
    } 

    $updated_times = array_values($times); 

    for ($index = 0; $index < count($updated_times); $index++){ 
     echo "<option value=$updated_times[$index]>$updated_times[$index]</option>"; 
    } 

    echo "</select>"; 

?> 

+0

耶代码只是关闭我的头顶..我应该先试过,但是谢谢。 – vinstah 2012-04-24 11:06:47

+0

如果您发现答案足够了,请您接受。 – dirtyhandsphp 2012-04-24 11:32:01