Covert mysql to postgresql query - mysql

I have an example that works perfectly in mysql but when I converts to postgresql don't works.
Mysql Code: (index.php):
<?php
require('includes/connection.php');
include('func.php');
?>
<html>
<head>
</head>
<body>
<p>
<form action="" method="post">
<select name="drop_1" id="drop_1">
<option value="" selected="selected" disabled="disabled">Select a Category</option>
<?php getTierOne(); ?>
</select>
</form>
</body>
</html>
Mysql code(func.php):
function getTierOne()
{
$result = mysql_query("SELECT DISTINCT tier_one FROM three_drops")
or die(mysql_error());
while($tier = mysql_fetch_array( $result ))
{
echo '<option value="'.$tier['tier_one'].'">'.$tier['tier_one'].'</option>';
}
}
POstgreSql Code: (index.php):
<?php
require('includes/connection.php');
include('func.php');
?>
<html>
<head>
</head>
<body>
<p>
<form action="" method="post">
<select name="drop_1" id="drop_1">
<option value="" selected="selected" disabled="disabled">Select a Category</option>
<?php getTierOne(); ?>
</select>
</form>
</body>
</html>
PostgreSql code(func.php):
function getTierOne()
{
$sth = $dbh->query("SELECT DISTINCT tier_one FROM three_drops");
while($tier = $sth->fetch())
{
echo '<option value="'.$tier['tier_one'].'">'.$tier['tier_one'].'</option>';
}
}
Connections (Postgresql):
require("constants.php");
$dbh = new PDO('pgsql:host=' . DB_SERVER . ';dbname=' . DB_NAME, DB_USER, DB_PASS,
array (PDO::ATTR_PERSISTENT => true ));
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
The connection is working but I get an error: Call to a member function query() on a non-object.

Try defining the $dbh global.
connection.php:
require("constants.php");
global $dbh;
func.php:
function getTierOne()
{
global $dbh;

Related

how to keep selected value in select box after submit or page reload were values and option are different which retrieved from db?

I want to keep selected particular value which was selected before the form submission. But after reloading or submisssion i did not get the previous selected value.help me!
<select name="select" class="dropbtn" onchange="this.form.submit()">
<?php
include 'connect.php';
$query="SELECT * FROM `user` WHERE id =1";
$result=mysqli_query($link_id,$query);
while($data = mysqli_fetch_assoc($result))
{ ?>
<option value="<?php echo $data['user_id']; ?>"> <?php echo
$data['address']; ?> </option>
<?php } ?>
</select>
<?php
$id = null;
if(isset($_POST['select']))
{
$id = $_POST['select'];
echo $id;
}
?>

Show the selected value everytime in the dropdown list

I need to display the selected category value displayed all the time. I have a list of category values and adding it to drop-down list from an array.
<form action="#" method="post">
<select name="dropDown" id="drop_down_id">
<option value=""> Select CATEGORY</option>
<?php
foreach($decoded as $key => $value ){
foreach($value as $key1 => $value1 ){
?> <option value="<?php echo $value1;?>"><?php echo $value1;?></option><?php
}
}
?>
<input type="submit" name="submit" value="Submit"/>
</form>
I see that you use post for form so you can use POST to know last selected:
$dropDown=htmlentities($_POST['dropDown'], ENT_QUOTES, "UTF-8");
And then just simple make if statement and its done:
if($dropDown==$value1){$selected='selected';}else{$selected=null;}
result:
<?php
$dropDown=htmlentities($_POST['dropDown'], ENT_QUOTES, "UTF-8");
?>
<form action="#" method="post">
<select name="dropDown" id="drop_down_id">
<option value=""> Select CATEGORY</option>
<?php
foreach($decoded as $key => $value ){
foreach($value as $key1 => $value1 ){
if($dropDown==$value1){$selected='selected';}else{$selected=null;}
echo '<option value="'.$value1.'" '.$selected.'>'.$value1.'</option>';
}
}
?>
<input type="submit" name="submit" value="Submit"/>
</form>
You can also store it in PHP SESSSION http://php.net/manual/en/function.session-start.php

Send data to the server with AJAX

I want some help with the following issue. The following code is not working for me. Is there anyone who can help me with that? I've watched this in a youtube video and i can't find why it is not running..
I want to inform you that i am running a WAMP Server at 127.0.0.1 and it is very weird that the browser does not give me any response for errors, etc..
Here is my PHP code:
<?php
$host="127.0.0.1";
$username = "root";
$password = "";
$db = "data";
$conn = mysqli_connect($host, $username, $password, $db);
if (isset($_POST['fname'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['slct'];
$query = "Insert into users (firstname, lastname,gender) Values(?,?,?))";
$stmt = $conn->prepare($query);
$stmt->bind_param('sss', $fname, $lname, $gender);
$stmt->execute();
if(mysqli_affected_rows($conn) > 0){
echo "insert";
}
else{
echo "no";
}
}
?>
Here is my html code:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<h3> Insert Data into Database</h3>
<form id="form1" method="post">
<p> Enter FirstName</p>
<input type="text" name="fname" class="form-control">
<br>
<p> Enter lastName</p>
<input type="text" name="lname" class="form-control">
<br>
<p>Enter Gender</p>
<select class="form-control" name="slct">
<option value="Male">Male </option>
<option value="Female">Female</option>
</select>
<br><br><br>
<button class="btn btn-primary" onclick="insertData()">Submit</button>
</form>
</body>
<script>
function insertData(){
var formData = $('#form1').serialize();
$.ajax({
url:'http://127.0.0.1/PHP/insert.php',
data:formData,
type:'Post',
success:function(response){
console.log(response);
},
error:function(err){
console.log(err);
}
})
}
</script>
</html>
Any help will be appreciate.
Thank you in advance!
Your insert-command is incorrect. Change ...(?,?,?))"; to ...(?,?,?)";
Better use an IDE like netbeans, because they would highlight such errors. Also you should switch from mysqli to PDO. And you don't need to check affected rows. The $stmt->execute() returns a boolean

Multiple options for query from select menu

I have a search area which contains 3 field, one is an input field and the other two are drop down menus. Either someone enters something into the input field or they select from BOTH of the drop downs (state and city).
Input search works fine. Select menus do not.
Here is the form.
<form name="ffl_finder" method="get" action="index.php">
Enter a Zip Code to find a Firearms Dealer near you: <br />
<input name="q" type="text" id="q" size="40">
Select a State AND a City<br />
<select name="qoption2">
<option value="" selected="selected">Select . . .</option>
<?php
$sql2 = "SELECT DISTINCT state from ffl_list order by state asc";
$rs_results2 = mysql_query($sql2) or die(mysql_error());
while ($rrows2 = mysql_fetch_array($rs_results2)) {
?>
<option value="<?php echo $rrows2[state]; ?>"><?php echo $rrows2[state]; ?></option>
<?php
}
?>
</select>
<select name="qoption3">
<option value="" selected="selected">Select . . .</option>
<?php
$sql3 = "SELECT DISTINCT city from ffl_list order by city asc";
$rs_results3 = mysql_query($sql3) or die(mysql_error());
while ($rrows3 = mysql_fetch_array($rs_results3)) {
?>
<option value="<?php echo $rrows3[city]; ?>"><?php echo $rrows3[city]; ?></option>
<?php
}
?>
</select>
<input name="doSearch" type="submit" id="doSearch" value="Search">
</form>
Here is the query:
<?php if ($get['doSearch'] == 'Search') {
//$cond2 = '';
if($get['qoption2'] != '') {
$cond2 = "`state` = 'qoption2'";
}
//$cond3 = '';
if($get['qoption3'] != '') {
$cond3 = "`city` = 'qoption3'";
}
if($get['q'] == '') {
$sql = "select * from ffl_list where $cond2 and $cond3 order by state asc, city asc";
}
else {
$sql = "select * from ffl_list where `zip` = '$_REQUEST[q]' ORDER BY `id` asc";
}
$rs_results = mysql_query($sql) or die(mysql_error());
} ?>
I need the query to search both city and state and I cannot seem to get the conditions right. I know this is a lot of code but if someone could help that would be great.
Thanks,
Clint
<?php
while ($rrows2 = mysql_fetch_array($rs_results2))
{
?>
<option value="<?php echo $rrows2['state']; ?>"><?php echo $rrows2['state']; ?></option>
<?php
}
?>
state and citymay be cols' names of the ffl_list table, you have to use a string a key of the returned array from mysql_Fetch_array. You did wrong for $rrows2 and $rrows3
When you create your filter for the query, you set a hard string, not GETted data :
$cond2 = '';
if($_GET['qoption2'] != '') {
$cond2 = "`state` = '".$_GET['qoption2']."'";
}

no mysql insert if header() is added

I'm trying to get my form to submit and then use header() to redirect.
MYSQL update works fine as long as I DON'T include header('location: setup2.php'); on line 9. When I add it, the redirect works, but the MYSQL doesn't update. Ideas?
<?php
$link;
#mysql_select_db(stevensp_beattrack) or die( "Unable to select database");
$inst=$_POST['inst'];
$id=$_SESSION['user_id'];
if(isset($_POST['submit'])) {
$query = "UPDATE users SET inst='$inst' WHERE id=$id";
mysql_query($query);
header('location: setup2.php'); //adding this line stops the update from working
}
?>
<HTML>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<select name="inst">
<option value="None">Select a Specialty:</option>
<option value="Voice">Voice</option>
<option value="Guitar">Guitar</option>
</select><br/><br/>
<input type="submit" name="submit" value="Next"/>
</form>
</HTML>