数据库查询与PHP不兼容

时间:2017-07-24 20:19:14

标签: php mysql

大家晚上好,在学习的时候我遇到了PHP if,else和elseif语句。我试图把if和else用,但IF语句真的不起作用。也许我错过了一个小细节。该代码用于检查用户名是否已在表中,然后跳过。任何帮助将不胜感激。如果这里已经回答了这个问题,请指点我的URL

<?php
include 'inc.dbcon.php';
// Report all errors
error_reporting(E_ALL);

$username = mysqli_real_escape_string($con, $_POST['username'][0]);
$school = mysqli_real_escape_string($con, $_POST['school'][0]);
$candname = $_POST['candname'];

$query = mysqli_query($con, "SELECT * FROM parlia_votes WHERE username='".$username."'");
if(mysqli_num_rows($query) > 0) {
?>
<script>
    alert('Vote has already been cast.');
    window.location = 'logout.php'; 
</script>
<?php
}
else
    foreach ($_POST['candname'] as $candname) {
        $query = sprintf("INSERT INTO parlia_votes (username, school, candname) VALUES ('%s', '%s', '%s')", $username, $school, $candname);
        ?>
        <script>alert('Thank you for casting your votes.');</script>
        <script>window.location = 'logout.php'; </script>
        <?php
        $con->query($query);
    }
?>

PS:我正在使用foreach循环发布到数据库。感谢

0 个答案:

没有答案