使用pdo / php在表中插入数据

时间:2015-01-12 12:50:46

标签: php mysql pdo

我需要帮助,我的代码不能正常工作,它不会插入到表db中。我认为我的代码编写得不好。我需要工作的部分是第一个开关盒!

我需要它在pdo中,我的数据库和表数据是正确的。

<?php

if (isset($_GET['poslovni_korisnici'])) {
$active1 = 'class="active"';
}
else {
$active1 = '';
}
if(isset($_GET['korisnici'])) {
$active2 = 'class="active"';
}
else {
$active2 = '';
}

/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'root';

/*** mysql password ***/
$password = '';

try {
$dbh = new PDO("mysql:host=$hostname;dbname=zadatak1", $username, $password);
/*** echo a message saying we have connected ***/
/**echo 'Connected to database';**/
}
catch(PDOException $e)
{
echo $e->getMessage();
}

echo'
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Statistika</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet">

    <!-- DataTables CSS -->
    <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css">

    <!-- jQuery -->
    <script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script>

    <!-- DataTables -->
    <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesnt work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>

    <nav class="navbar navbar-default">
    <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
    <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    </button>
    <a class="navbar-brand" href="index.php">POSLOVNA STATISTIKA</a>
    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav">
    <li ' . $active1 . '><a href="index.php?poslovni_korisnici">Poslovni korisnici <span class="sr-only">(current)</span></a></li>
    <li ' . $active2 . '><a href="index.php?korisnici">Korisnici</a></li>
    </ul>
    </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
    </nav>';

        switch (isset($_GET)) {
            case isset($_GET['unosPoslovni']):

                if(isset($_POST['submit1']))
                    {


                    $partner_name    = $_POST['Partner_name'] = NULL;
                    $partner_street  = $_POST['Partner_street'] = NULL;
                    $partner_zip     = $_POST['Partner_zip'] = NULL;
                    $partner_city    = $_POST['Partner_city'] = NULL;
                    $partner_country = $_POST['Partner_country'] = NULL;

                    $sql = "INSERT INTO poslovni_partneri 
                           (Partner_name,Partner_street, Partner_zip, Partner_city, Partner_country) 
                           VALUES('$partner_name','$partner_street',$partner_zip, $partner_city, $partner_country";                         
                    $result = mysql_query($sql);            
                  }         
                echo'
                <h1>Unos poslovni korisnika</h1>
                </br>
                </br>

                <form action="unos.php?unosPoslovni" method="POST" target="_parent" accept-charset="UTF-8">
                <table id="table_id" class="display">
                    <thead>
                        <tr>
                          <th>Partner name</th>
                          <th>Partner street</th>
                          <th>Partner zip</th>
                          <th>Partner city</th>
                          <th>Partner country</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input type="text" name="partner_name" value=""></td>
                            <td><input type="text" name="partner_street" value=""></td>
                            <td><input type="text" name="partner_zip" value=""></td>
                            <td><input type="text" name="partner_city" value=""></td>
                            <td><input type="text" name="partner_country" value=""></td>
                        </tr> 
                    </tbody>
                </table>        
                <input type="submit" name="submit1" value="Spremi" />
                </form>';



                break;

            default:
                # code...
                break;
        }

        switch (isset($_GET)) {
            case isset($_GET['unosKorisnici']):

                echo'
                <h1>Unos korisnika</h1>
                </br>
                </br>

                <form action="unos.php?unosPoslovni" method="POST" target="_parent" accept-charset="UTF-8">
                <table id="table_id" class="display">
                    <thead>
                        <tr>
                          <th>User name</th>
                          <th>User department</th>
                          <th>User email</th>
                          <th>User phone</th>
                          <th>User mobile</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input type="text" name="user_name" value=""></td>
                            <td><input type="text" name="user_department" value=""></td>
                            <td><input type="text" name="user_email" value=""></td>
                            <td><input type="text" name="user_phone" value=""></td>
                            <td><input type="text" name="user_mobile" value=""></td>
                        </tr> 
                    </tbody>
                </table>
                </form>';

                break;

            default:
                # code...
                break;
        }


    echo'
    <script>
      $(document).ready( function () {
          $("#table_id").DataTable();
      } );
    </script>

    <!-- jQuery (necessary for Bootstraps JavaScript plugins) -->
    <!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> -->
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>';    

?>

1 个答案:

答案 0 :(得分:2)

您的SQL查询无效,因为它缺少右括号());

INSERT INTO poslovni_partneri(Partner_name,Partner_street, Partner_zip, Partner_city,     Partner_country) 
VALUES('$partner_name','$partner_street',$partner_zip, $partner_city, $partner_country";

看起来应该更像这样:

INSERT INTO poslovni_partneri(Partner_name,Partner_street, Partner_zip, Partner_city,     Partner_country) 
VALUES('$partner_name','$partner_street','$partner_zip', '$partner_city', '$partner_country')";

你应该与引语保持一致,你有时会围绕变量使用它,有时候不会。但/ p>

此外,你甚至不应该这样查询;您应该使用prepared statements,因为这样您就可以SQL injections打开。

修改

您说您需要PDO,您正在建立PDO连接但是您使用mysql_*特定功能进行查询,这些功能您应该如何使用PDO并与PDO无关。

修改2

您还应该验证帖子参数是否实际可用,使用您的帖子参数执行类似的操作:

$partner_name = isset($_POST['partner_name']) ? $_POST['partner_name'] : '';

注意小写p

使用ternary operator,并对所有post参数执行此操作,如果任何参数为空字符串,则显示错误而不是查询。

编辑3

正如fred所指出的,partner_name!= Partner_name,即您的name属性标识符(在html中)与您尝试匹配的属性标识符不匹配在后论据中。