no mysql insert if header() is added - mysql

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>

Related

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

select tag in form not working

I have a form and a drop-down list inside it...
<form method="post" action="rew1.php" id="inputform">
<select form="inputform" name="some1">
<option value="" disabled="disabled" selected="selected">Select Travel Mode</option>
<option value="Train">Train</option>
<option value="Flight">Flight</option>
</select>
</form>
But when i post using this form the value Train Or Flight is not posted in to the database....
Here's The Server Side Code...
<?php
$con=mysqli_connect("127.0.0.1","root","plsdonthack","cab");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO cabs (NAME,IDNO,HOSTEL,MODE,DATE,TIME,TFNO,CONTACT)
VALUES
('$_POST[name]','$_POST[idno]','$_POST[hostel]','$_POST[mode]','$_POST[date]','$_POST[time]','$_POST[tfno]','$_POST[contact]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
header("location:http://localhost\overflow\index.html");
?>
What is the mistake i am making....?
you should set name attribute for select like this:
<select name="something">

Append Select List value to URL

I have a form that is nothing more than a select list that is displaying records from a database and a button. All I want to do is when they select an option and click Submit, it takes them to a page that deletes the record in question. To delete it - I need the tech_id (from the select list) to be appended to the URL. I've done it the way I would with a text field, but that didn't work. Any suggestions?
<form method="post" id="form1" name="form1" action="delete-tech.php?tech_id=<?php echo $_POST['technician']; ?>">
<p>Choose Technician to Delete:
<select name="technician" id="technician" title="technician">
<?php
do {
?>
<option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
<?php
} while ($row_getTechs = mysqli_fetch_assoc($getTechs));
$rows = mysqli_num_rows($getTechs);
if($rows > 0) {
mysqli_data_seek($getTechs, 0);
$row_getTechs = mysqli_fetch_assoc($getTechs);
}
?>
</select>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Delete Technician">
</p>
</form>
The error lies in the coding construct.
I have slightly changed the position of a code segment following while statement as follows(i.e. placed it before the do phrase):
<form method="GET" id="form1" name="form1" action="delete-tech.php">
<p>Choose Technician to Delete:
<select name="technician" id="technician" title="technician">
<?php
$rows = mysqli_num_rows($getTechs);
if($rows > 0) {
mysqli_data_seek($getTechs, 0);
$row_getTechs = mysqli_fetch_assoc($getTechs);
do {
?>
<option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
<?php
}while ($row_getTechs = mysqli_fetch_assoc($getTechs));
}
?>
</select>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Delete Technician">
</p>
</form>
It should append the information(list-value) from the form to the URL as you desired. Check it out.
As you say it's not working there is another way using JavaScript:
<form method="post" id="form1" name="form1" action="delete-tech.php">
<p>Choose Technician to Delete:
<select name="technician" id="technician" title="technician">
<?php
$rows = mysqli_num_rows($getTechs);
if($rows > 0) {
mysqli_data_seek($getTechs, 0);
$row_getTechs = mysqli_fetch_assoc($getTechs);
do {
?>
<option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
<?php
}while ($row_getTechs = mysqli_fetch_assoc($getTechs));
}
?>
</select>
</p>
<p>
<input name="submit" type="button" id="submit" value="Delete Technician" onclick="location.href='delete-tech.php?tech_id='+document.getElementById('technician').value">
</p>
</form>

Covert mysql to postgresql query

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;