我需要幫助,我的代碼不工作,它不會插入到表db中。我認爲我的代碼寫得不好。我需要工作的部分是第一個開關盒!使用pdo/php插入數據表
我需要它在pdo中,我的db和表數據是正確的。
<?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>';
?>
我看到PDO,我看到'mysql_',我看到沒有引用的值,缺少括號,還有什麼可能出錯?問你自己那個問題;-) –
我按提交,什麼也沒有發生 – Vorda
你正在混合'mysql_'函數和PDO。僅使用PDO。 –