Php form filling mySQL table rows with 1's - php

So I'm making a complaints page for a school project. I started off with a simple PHP form, which worked until I added a
if(isset($_POST['submit'])){ PHP code to be executed here }
Which didnt work anymore. So I decided to remove that line all together; and whenever I submit any form now, it fills in my MYsql table with 1's even though I am filling the text boxes of the form with text. I don't know if this a problem in mySQL configuration, I have another form that is suffering from the same issue.
I will describe the issue in more details below.
How my "Klacht" Table currently looks like (Excuse my links issue.. I can't post pictures because I dont have more than 10 reps..)
http://i.gyazo.com/6589558c59c4955f5cd48c335d79bdac.png
The structure of it
http://gyazo.com/51ab9d9184a4beb2197ce41f0b98b35b
My form code is a .php file, and I don't get a preview of my PHP code upon pressing the submit button. It just echos the message
<!DOCTYPE html>
<html lang="nl">
<head>
<title>Prototype</title>
</head>
<body>
<h3>Klacht test</h3>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<br />Test nummer: <input type="text" name="Nr">
<br />Postcode<input type="text" name="Postcode">
<br />Datum<input type="Date" name="Datum">
<br />Tijd<input type="text" name="Tijd" >
<br />Soort klacht
<select name="Soort">
<option value=" "></option>
<option value="Geluid">Geluid</option>
<option value="Milieu">Milieu</option>
<option value="Veiligheid">Veiligheid</option>
</select>
<!-- Submit button -->
<br /><input type="submit" value="Versturen">
<input type="reset" value="Reset">
</form>
</body>
<?php
mysql_connect("localhost", "root", "") or die ("Could not connect to database");
mysql_select_db("schiphol") or die("could not select database");
$Nr = isset($_POST["Nr"]);
$Postcode = isset($_POST["Postcode"]);
$Datum = isset($_POST["Datum"]);
$Tijd = isset($_POST["Tijd"]);
$Soort = isset($_POST["Soort"]);
$query = ("INSERT INTO klacht (Nr, Postcode, Datum, Tijd, Soort)
VALUES ('$Nr', '$Postcode', '$Datum', '$Tijd', '$Soort')");
$resultaat = mysql_query($query) or die (mysql_error( ));
if($resultaat) echo "Uw klacht is toegevoegd" ; else echo "ERROR";
?>
</html>
So.. What am I doing wrong? As I mentioned above, another form is suffering from the same issue. It used to work but doesn't anymore.
Thanks in advance.

Your problem on isset() it return 0 or 1
You use blow code
<input type="submit" value="Versturen">
but you forgot to mention name
<input type="submit" name="submit" value="Versturen">
Try this it will work
<!DOCTYPE html>
<html lang="nl">
<head>
<title>Prototype</title>
</head>
<body>
<h3>Klacht test</h3>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<br />Test nummer: <input type="text" name="Nr">
<br />Postcode<input type="text" name="Postcode">
<br />Datum<input type="Date" name="Datum">
<br />Tijd<input type="text" name="Tijd" >
<br />Soort klacht
<select name="Soort">
<option value=""></option>
<option value="Geluid">Geluid</option>
<option value="Milieu">Milieu</option>
<option value="Veiligheid">Veiligheid</option>
</select>
<!-- Submit button -->
<br /><input type="submit" name="submit" value="Versturen">
<input type="reset" value="Reset">
</form>
</body>
<?php
if(isset($_POST['submit'])){
echo "<pre/>";
print_r($_POST);
//die;
mysql_connect("localhost", "root", "") or die ("Could not connect to database");
mysql_select_db("schiphol") or die("could not select database");
$Nr = $_POST["Nr"];
$Postcode = $_POST["Postcode"];
$Datum = $_POST["Datum"];
$Tijd = $_POST["Tijd"];
$Soort = $_POST["Soort"];
$query = ("INSERT INTO klacht (Nr, Postcode, Datum, Tijd, Soort) VALUES ('$Nr', '$Postcode', '$Datum', '$Tijd', '$Soort')");
echo $query;
$resultaat = mysql_query($query) or die (mysql_error( ));
if($resultaat) echo "Uw klacht is toegevoegd" ; else echo "ERROR";
}
?>

Hi I think your problem is with isset() funtion. Actually isset() funtion will return a Boolean value based 1 or 0 . so please update your php section without isset() like below and try.
<?php
mysql_connect("localhost", "root", "") or die ("Could not connect to database");
mysql_select_db("schiphol") or die("could not select database");
$Nr =$_POST["Nr"];
$Postcode =$_POST["Postcode"];
$Datum = $_POST["Datum"];
$Tijd = $_POST["Tijd"];
$Soort = $_POST["Soort"];
$query = ("INSERT INTO klacht (Nr, Postcode, Datum, Tijd, Soort)
VALUES ('$Nr', '$Postcode', '$Datum', '$Tijd', '$Soort')");
$resultaat = mysql_query($query) or die (mysql_error( ));
if($resultaat) echo "Uw klacht is toegevoegd" ; else echo "ERROR";
?>

$var = is set($_POST['some_var']); // this will just return a 1 to $var if the post variable is set
Try this instead
if(isset($_POST['var'])){ $db_var = $_POST['var'];}

The following if(isset($_POST['submit'])){ PHP code to be executed here } did not work because you did not give a name to your button
<input type="submit" value="Versturen">
also as #Azeez Kallayi pointed out isset() returns boolean value.
Try this:
<?php
if(isset($_POST["Nr"],$_POST["Postcode"],$_POST["Datum]",$_POST["Tijd"],$_POST["Soort"])){
mysql_connect("localhost", "root", "") or die ("Could not connect to database");
mysql_select_db("schiphol") or die("could not select database");
$Nr = mysql_real_escape_string($_POST["Nr"]);
$Postcode = mysql_real_escape_string($_POST["Postcode"]);
$Datum = mysql_real_escape_string($_POST["Datum"]);
$Tijd = mysql_real_escape_string($_POST["Tijd"]);
$Soort = mysql_real_escape_string($_POST["Soort"]);
$query = ("INSERT INTO klacht (Nr, Postcode, Datum, Tijd, Soort)
VALUES ('$Nr', '$Postcode', '$Datum', '$Tijd', '$Soort')");
$resultaat = mysql_query($query) or die (mysql_error( ));
if($resultaat) echo "Uw klacht is toegevoegd" ; else echo "ERROR";
}else{?>
<!DOCTYPE html>
<html lang="nl">
<head>
<title>Prototype</title>
</head>
<body>
<h3>Klacht test</h3>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<br />Test nummer: <input type="text" name="Nr">
<br />Postcode<input type="text" name="Postcode">
<br />Datum<input type="Date" name="Datum">
<br />Tijd<input type="text" name="Tijd" >
<br />Soort klacht
<select name="Soort">
<option value=" "></option>
<option value="Geluid">Geluid</option>
<option value="Milieu">Milieu</option>
<option value="Veiligheid">Veiligheid</option>
</select>
<!-- Submit button -->
<br /><input type="submit" value="Versturen">
<input type="reset" value="Reset">
</form>
</body>
</html>
<?php}?>
<?php
if(isset($_POST["Nr"],$_POST["Postcode"],$_POST["Datum]",$_POST["Tijd"],$_POST["Soort"])){
mysql_connect("localhost", "root", "") or die ("Could not connect to database");
mysql_select_db("schiphol") or die("could not select database");
$Nr = mysql_real_escape_string($_POST["Nr"]);
$Postcode = mysql_real_escape_string($_POST["Postcode"]);
$Datum = mysql_real_escape_string($_POST["Datum"]);
$Tijd = mysql_real_escape_string($_POST["Tijd"]);
$Soort = mysql_real_escape_string($_POST["Soort"]);
$query = ("INSERT INTO klacht (Nr, Postcode, Datum, Tijd, Soort)
VALUES ('$Nr', '$Postcode', '$Datum', '$Tijd', '$Soort')");
$resultaat = mysql_query($query) or die (mysql_error( ));
if($resultaat) echo "Uw klacht is toegevoegd" ; else echo "ERROR";
}else{?>
<!DOCTYPE html>
<html lang="nl">
<head>
<title>Prototype</title>
</head>
<body>
<h3>Klacht test</h3>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<br />Test nummer: <input type="text" name="Nr">
<br />Postcode<input type="text" name="Postcode">
<br />Datum<input type="Date" name="Datum">
<br />Tijd<input type="text" name="Tijd" >
<br />Soort klacht
<select name="Soort">
<option value=" "></option>
<option value="Geluid">Geluid</option>
<option value="Milieu">Milieu</option>
<option value="Veiligheid">Veiligheid</option>
</select>
<!-- Submit button -->
<br /><input type="submit" value="Versturen">
<input type="reset" value="Reset">
</form>
</body>
</html>
<?php}?>

Related

How to make this auto populate multiple input based on dropdown selection save more than ten(10) in mysql usingPHP

I created an auto populate multiple input fields based on the dropdown selection. My problem is the 2nd select box is not populating the corresponding input fields. I know this is wrong because the ID's are the same from select1 but how can I make it work? I need to create around 20 or more select dropdown and I can't figure out how to fix it.
<?php
$con = mysql_connect("localhost","root","");
if (!$con) { die('Could not connect: ' . mysql_error()); }
$db = mysql_select_db("pbiees") or die('Could not select DB: ' . mysql_error());
?>
<html>
<head>
<script type="text/javascript">
var compInfoArray = new Array();
<?php
$query1 = "SELECT * FROM subject ORDER BY course_id";
$result1 = mysql_query($query1) or die(mysql_error());
// build javascript array
while($row1=mysql_fetch_array($result1)){
echo 'compInfoArray['.$row1['subj_id'].'] = new Array();';
echo 'compInfoArray['.$row1['subj_id'].']["course_code"] = "'.$row1['course_code'].'";';
echo 'compInfoArray['.$row1['subj_id'].']["course_desc"] = "'.$row1['course_desc'].'";';
echo 'compInfoArray['.$row1['subj_id'].']["pr"] = "'.$row1['pr'].'";';
echo 'compInfoArray['.$row1['subj_id'].']["unit"] = "'.$row1['unit'].'";';
}
?>
function showname() {
var subj_id = document.form1.subj_id.value;
document.form1.course_code.value = compInfoArray[subj_id]["course_code"];
document.form1.course_desc.value = compInfoArray[subj_id]["course_desc"];
document.form1.pr.value = compInfoArray[subj_id]["pr"];
document.form1.unit.value = compInfoArray[subj_id]["unit"];
}
window.onload=function() {
showname();
}
</script>
</head>
<body>
<form name="form1">
<select name="subj_id" onchange="showname()">
<?php
$query1 = "SELECT * FROM subject ORDER BY course_id";
$result1 = mysql_query($query1) or die(mysql_error());
// build javascript array
while($row1=mysql_fetch_array($result1)){
echo '<option value="'.$row1['subj_id'].'">'.$row1['course_code'].'</option>';
}
?>
</select>
<label>
<input type="text" name="course_code" value="" />
<input type="text" name="course_desc" value="" />
<input type="text" name="pr" value="" />
<input type="text" name="unit" value="" />
</label>
<select name="subj_id" onchange="showname()">
<?php
$query1 = "SELECT * FROM subject ORDER BY course_id";
$result1 = mysql_query($query1) or die(mysql_error());
// build javascript array
while($row1=mysql_fetch_array($result1)){
echo '<option value="'.$row1['subj_id'].'">'.$row1['course_code'].'</option>';
}
?>
</select>
<label>
<input type="text" name="course_code" value="" />
<input type="text" name="course_desc" value="" />
<input type="text" name="pr" value="" />
<input type="text" name="unit" value="" />
</label>
</form>
</body>
<html>

Populate Radio buttons and submit value to query database

I am trying to learn programming and have to populate a set of radio buttons and submit what is selected to show records from a database. I have already done this with a selection list, but can't quite understand what I need to change to convert it to a radio buttons.
Selection list:
<?php
require_once("dbconn.php");
$sql = "SELECT staffName, staffID FROM staff";
$rs = mysqli_query($dbConn, $sql) or die ('Problem with query' . mysqli_error($dbConn));
?>
<form id="task9" action="task7.php" method="get">
<select name="staffID" id="staffID">
<?php
while($row = mysqli_fetch_array($rs)) {
$name=$row["staffName"];
$staffIden=$row["staffID"];
echo "<option value=".$staffIden.">".$name."</option>";
}
?>
<br><br>
<input type="submit" name="submit" method="get">
<input type="reset" name="reset">
</form>
Radio buttons (all I get is all the names and only One radio button):
<?php
require_once("dbconn.php");
$sql = "SELECT staffName, staffID FROM staff";
$rs = mysqli_query($dbConn, $sql) or die ('Problem with query' . mysqli_error($dbConn));
?>
<form id="task9" action="task7.php" method="get">
<input type = "radio" name="staffID" id="staffID">
<?php
while($row = mysqli_fetch_array($rs)) {
$name=$row["staffName"];
$staffIden=$row["staffID"];
echo "<option value=".$staffIden.">".$name."</option>";
}
?>
Hopefully this question is clear enough.
Try using below code.
<?php
require_once("dbconn.php");
$sql = "SELECT staffName, staffID FROM staff";
$rs = mysqli_query($dbConn, $sql)
or die ('Problem with query' . mysqli_error($dbConn));
?>
<form id="task9" action="task7.php" method="get">
<?php
while($row = mysqli_fetch_array($rs)) {
$name=$row["staffName"];
$staffIden=$row["staffID"];
echo "<label>";
echo "<input type='radio' name='staffID' value='".$staffIden."'/> ";
echo $name;
echo "</label><br/>";
}
?>
<br><br>
<input type="submit" name="submit" method="get">
<input type="reset" name="reset">
</form>
try this
<?php
require_once("dbconn.php");
$sql = "SELECT staffName, staffID FROM staff";
$rs = mysqli_query($dbConn, $sql)
or die ('Problem with query' . mysqli_error($dbConn));
?>
<form id="task9" action="task7.php" method="get">
<?php
while($row = mysqli_fetch_array($rs)) {
$name=$row["staffName"];
$staffId=$row["staffID"];
?>
<input type='radio' name='staffID' value='<?php echo $staffId ?>'/>
<?php echo $name; ?>
<br/>
<?php
}
?>
<br/><br/>
<input type="submit" name="submit" method="get">
<input type="reset" name="reset">
</form>
to add to what is there you will likely need to add either some code to your task7.php file to handle the database actions or if the file is task7.php you will need to add a block to the top of your file to handle the self-submitted form

How to make PHP drop down menu be taken as a HTML form input

So I've made a drop down menu (using PHP), and I'm not sure how to make it so that it can be taken as an input into a HTML form?
This is the PHP code:
<?php
$dropdownsql = "SELECT prodName FROM tblProduct";
$dropdownresult = #mysqli_query($connect, $dropdownsql);
echo "<select name='prodID'>";
while ($row = mysqli_fetch_array($dropdownresult, MYSQLI_ASSOC)) {
echo "<option value='" . $row['prodID'] . "'>" . $row['prodName'] . "</option>";
}
echo "</select>";
?>
And this is the HTML form code:
<form action="addOrder.php" method="post">
<p>
Order Date*:
<input type="text" name="orderDate" maxlength='70' required>
<p>
Order Location*:
<input type="text" name="orderLocation" maxlength='255' required>
<p>
Order Product*:
<p>
Order System*:
<input type="text" name="orderSystem" required>
<p>
<input type="submit" name="submit" value="Add Order">* Means that the field is required.
<input type="hidden" name="submitted" value="TRUE">
</form>
*Note that I want the input for Order Product
I have rewritten this to make it secure. Check if it works for you:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Link Games</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
include 'header.php';
include 'menu.php';
try {
$connect = new PDO("mysql:dbhost=localhost;dbname=linkgamesDB", 'username', 'password');
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
<div id="content">
<p class="headers"><strong>Add Order</strong></p>
<?php
#Check that the form is submitted
if (isset($_POST['submitted'])) {
#Create then run the query
$query= $connect->prepare("INSERT INTO tblOrder(orderDate, orderLocation, orderProd, orderSystem) VALUES (?, ?, ?, ?)")
$query->bindValue(1, $_POST['orderDate']);
$query->bindValue(2, $_POST['orderLocation']);
$query->bindValue(3, $_POST['orderProd']);
$query->bindValue(4, $_POST['orderSystem']);
$query->execute();
#Check if there is an error in query and display relevant message
if ($query->rowCount() > 0) {
echo '<p class="records">Thank you! Record Added.</p>';
} else {
echo '<p class="records">Error. No record added.</p>';
}
}
?>
<form action="addOrder.php" method="post"><p>
Order Date*: <input type="text" name="orderDate" maxlength='70' required><p>
Order Location*: <input type="text" name="orderLocation" maxlength='255'required><p>
Order Product*: <select name='orderProd'>
<?php
$dropdownsql = $connect->prepare("SELECT prodID, prodName FROM tblProduct");
$dropdownsql->execute();
$r = $dropdownsql->fetchAll();
foreach($r AS $row):
?>
<option value="<?php echo $row['prodID'];?>"><?php echo $row['prodName'];?></option>
<?php endforeach;?>
</select>
<p>
Order System*: <input type="text" name="orderSystem" required><p>
<input type="submit" name="submit" value="Add Order"> * Means that the field is required.
<input type="hidden" name="submitted" value="TRUE">
</form>
</div>
</body>
</html>

Error with insert statement from PHP to MS Access

I've successfully login with a user account and now i will like to insert records. As below is the code that has not been successful in inserting record.
<?php
session_start();
echo "Welcome: ". $_SESSION['role'];
?>
<?php
error_reporting(0);
if (!$_POST['submit'])
{
?
<html>
<body>
<br><br>
<fieldset >
Add a new user
<br>
<br>
<label for='username'>Username: </label>
<input type='text' name='username' id='username'/>
<label for='password'>Password: </label>
<input type='password' name='password' id='password' maxlength="50" />
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Role:
<br>
<select name="role">
<option value="" selected="selected"></option>
<option VALUE="Administrator"> Administrator</option>
<option VALUE="Secretary"> Secretary</option>
<option VALUE="Employee"> Employee</option>
</select>
<input type='submit' name='Submit' value='Submit' />
</form>
</fieldset>
<table width=100%>
</html>
</body>
<?php
}
else
{
$conn=odbc_connect("employee","","") or die (odbc_errormsg());
if (!$conn)
{
exit
("Connection Failed: " . $conn);
}
$query = "INSERT INTO empTable (empID, password, Role, Days left in MC, Days left in leave) VALUES" .
"('$_POST[username]', '$_POST[password]', '$_POST[role]', 14, 14)";
$result=odbc_exec($conn,$query) or die ("result error ".odbc_error().'-'.odbc_errormsg());
odbc_fetch_row($result);
odbc_close($conn);
}
After clicking the submit button and when i refresh my database, nothing comes out. Why is that so? Thanks alot
Access doesn't like column names with blanks. You have to mask them with [ ].
Change your INSERT query like this:
$query = "INSERT INTO empTable (empID, password, Role, [Days left in MC], [Days left in leave]) VALUES ..."

Sending id through select menu using PHP

here is my problem, I want to send id number through select menu using PHP.
Here is the code:
<form name="update" method="post" action="ex_update.php?id=<?php echo ((int)$_POST['get_id']); ?>">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="get_id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form>
I have tried but when I submit the id in the URL equals to zero. how can I send id to the URL??
here is the ex_update.php >>>
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$id = (int)$_GET["get_id"];
$name = mysql_real_escape_string( $_POST["name"] );
$query = "UPDATE test SET name='{$name}' WHERE id=={$id}";
if($run = mysql_query($query)){
}else{mysql_error();}
?>
Thanks in advance
You can use the form GET method
<form name="update" method="GET" action="ex_update.php">
You can access that select box value using $_GET['get_id'] in ex_update.php
Here is your First Page
Note action of form...
<form name="update" method="post" action="ex_update.php">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="get_id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form>
And here you can find ex_update.php. Note: $id = (int)$_POST["get_id"];
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$id = (int)$_POST["get_id"];
$name = mysql_real_escape_string( $_POST["name"] );
$query = "UPDATE test SET name='{$name}' WHERE id={$id}";
if($run = mysql_query($query)){
}else{mysql_error();}
?>

Categories