Reputation: 19
I need help, my code isn't working, it wont insert into table db. I think my code isn't written well. The part i need to work is first switch case!
I needed it to be in pdo, my db and table data is correct.
<?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>';
?>
Upvotes: 0
Views: 160
Reputation: 4967
Your SQL query is invalid since it's missing right parenthesis ( )
);
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";
Should look more like this:
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')";
You should be consistent with quotations aswell, you're sometimes using it around variables and sometimes not.
Furthermore, you shouldn't even be querying this way; you should be using prepared statements because this way you're open for SQL injections.
Edit
You say you need PDO, you're establishing a PDO connection but you're querying by using the mysql_*
specific functions, which is not how you should be using PDO and has nothing to do with PDO.
Edit 2
You should also validate whether the post arguments are actually available, do something like this with your post arguments:
$partner_name = isset($_POST['partner_name']) ? $_POST['partner_name'] : '';
(notice the lowercase p)
Using the ternary operator, and do this with all of the post arguments and if any of your arguments are the empty string, then display an error instead of querying.
Edit 3
As pointed out by fred, partner_name
!= Partner_name
, i.e. your name
attribute identifier (in the html) is not matching the ones you're trying to match with in the post arguments.
Upvotes: 2