what I'm trying to do is run a select statement for each answer to select the answer in the database where the questionID = $i and the userID = $userID so I have the query like this set up so far but not sure what I'm missing or am I right and not missing anything? Also no matter what i do both fields have values but I'm still getting the error message that I need to fill out both form fields.
<?php
$i = 1;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
?>
<dl>
<dt style="width: 190px;"><label for="answer[<?php echo $row['id']; ?>]"><?php echo $row['question'] ?></label></dt>
<dd><input type="text" name="answer<?php echo $i ?>[<?php echo $row['id']; ?>]" size="54" /></dd>
</dl>
<?php
++$i;
}
?>
if (empty($_POST['answer1'][$i]) || trim($_POST['answer1'][$i])=="") {$errors = "yes";}
if (empty($_POST['answer2'][$i]) || trim($_POST['answer2'][$i])=="") {$errors = "yes";}
// Error checking, make sure all form fields have input
if ($errors == "yes") {
// Not all fields were entered error
$message = "You must enter values to all of the form fields!";
$output = array('errorsExist' => true, 'message' => $message);
} else {
$userID = mysqli_real_escape_string($dbc,$_POST['userID']);
$answer1 = mysqli_real_escape_string($dbc,$_POST['answer1'][$i]);
$answer2 = mysqli_real_escape_string($dbc,$_POST['answer2'][$i]);
$query = "SELECT * FROM manager_users_secretAnswers WHERE questionID = '".$questionID."' AND userID = '".$userID."'";
$result = mysqli_query($dbc,$query);
echo $query;
You can have a problem with php type autoquessing. Let suppose thatyou have questions with ids: 3,5,7,8 then you are using:
empty($_POST['answer1'][$i])
$_POST['answer1'][3] so you are fetching third element of array.
So I suggest to use not array notation, but:
For input name: answer|${id} or answer_${id} instead of answer[$id]
Related
I creating an inventory web-base system by using php and php myadmin (InnoDB). I want to insert the value in inventory when I inserting the record, I can see the data of the (FK) in the dropdown but when I submit the form the data to the db, it returns as no input value in the field and the dropdown not there anymore. Is the way I'm using the foreign key in the dropdown wrong?
I have a table that contains multiple foreign keys.
Table Inventory(
id (pk),
name,
condition_(fk),
producttype (fk))
Table condition_type(
condition_ (pk))
Table producttype(
producttype(fk))
<?php
// Include config file
require_once "../config.php";
// Define variables and initialize with empty values
$name = $condition_ = $producttype = "";
$name_err = $condition_err = $producttype_err = "";
$sql2 = "SELECT * FROM condition_type";
$sql4 = "SELECT * FROM producttype";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate name
$input_name = trim($_POST["name"]);
if(empty($input_name)){
$name_err = "Please enter a name.";
}else{
$name = $input_name;
}
// Validate condition
$input_condition = trim($_POST["condition_"]);
if(empty($input_condition)){
$condition_err = "Please choose the condition.";
} else{
$condition_ = $input_condition;
}
// Validate producttype
$input_producttype = trim($_POST["prodcuttype"]);
if(empty($input_producttype)){
$producttype_err = "Please enter the product type..";
} else{
$producttype = $input_producttype;
}
// Check input errors before inserting in database
if(empty($name_err) && empty($condition_err) && empty($producttype_err)){
// Prepare an insert statement
$sql = "INSERT INTO inventory (name, condition_, producttype) VALUES (?, ?, ?)";
if($stmt = $mysqli->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bind_param("sss", $param_name, $param_condition, $param_producttype);
// Set parameters
$param_name = $name;
$param_condition = $condition;
$param_producttype = $producttype;
// Attempt to execute the prepared statement
if($stmt->execute()){
// Records created successfully. Redirect to landing page
header("location: ../application");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
$stmt->close();
}
// Close connection
$mysqli->close();
}
?>
-------> This is the form
<div class="form-group <?php echo (!empty($condition_err)) ? 'has-error'
: ''; ?>">
<label>Condition</label>
</br>
<select id="condition_" name="condition_" class="form-control" value="<?
php echo $condition_ ;?>">
<option>Please Select Product Condition</option>
<?php
if($result2 = $mysqli ->query($sql2)){
while($row = $result2->fetch_array()){
echo "<option value=".$row['condition_'].">" .$row['condition_']. "
</option>";
}
}
?>
</select>
<span class="help-block"><?php echo $condition_err;?></span>
</div>
<div class="form-group <?php echo (!empty($producttype_err)) ? 'has-
error' : ''; ?>">
<label>Product</label>
</br>
<select name="producttype" class="form-control" value="<?php echo
$producttype ;?>">
<?php if($result4 = $mysqli ->query($sql4)){
while($row = $result4->fetch_array()){
echo "<option value=".$row['producttype'].">" .$row['producttype']. "
</option>";
}
}
?>
</select>
<span class="help-block"><?php echo $manufacturer_err;?></span>
</div>
So when I submit it return as the condition and producttype are empty. I think the error is because of
}
// Close connection
$mysqli->close();
}
statement that already close. But I don't know to place it.
PHP Warning: mysqli::query(): Couldn't fetch mysqli in /home/my-path/application/create_new.php on line 173
You should debug the html form first by right click with 'Inspect element' and make sure that the 'Select' value is getting?
I need to echo a download button if there are certain correct results, but when I echo the download button it decides to echo more than 1. How do I correct this?
heres my code:
<?php
$u = $_SESSION["username"];
$getscripts = $conn->prepare("SELECT * FROM project_sa");
$getscripts->execute();
while ($row = $getscripts->fetch(PDO::FETCH_BOTH)) {
$sec = $conn->query('SELECT * FROM us WHERE username="wafflezzz"');
$sec->execute();
while ($rowx = $sec->fetch(PDO::FETCH_BOTH)) {
$checker = $rowx[$row["script_title"]];
if ($checker == $row["script_title"]) {
$geturl = $conn->prepare("SELECT * FROM project_sa WHERE script_title='$checker'");
$geturl->execute();
while ($row = $geturl->fetch(PDO::FETCH_BOTH)) {
echo '
<form method="post" action="dl.php">
<input name="bname" value="<?php echo $branded_m_img_url; ?>" hidden></input>
<input type="submit" class="ui huge button" value="Download"></input>
</form>';
}
}
}
}
?>
It returns about 2 broken duplicate entries when there is only 1 entry in the database!
"You have nested while loops, that's why you get duplicate entries, you also overwrite $row in the inner while loop – Alon Eitan yesterday"
After I fixed that, it worked!
The following function works fine despite having the "Undefined offset" error but i need to debug it still. Can someone help me spot where the error is?
This is the form :
$query = "SELECT * FROM application WHERE Shortlist_status = 1 AND Interview_datetime != '' AND Email_checked ='' ORDER BY Candidate_id ASC";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
while ($row = mysqli_fetch_array($result)) {
<input type="hidden" name="can_id[]" value=<?php echo $canid ?>>
<input type="hidden" name="job_id[]" value=<?php echo $jobid ?>>
(Some codes)
<input name="email[]" id="id" type="checkbox" value="1">
}
This is the form handling:
foreach ($_POST['can_id'] as $i => $candidate_id) {
$job_id = $_POST['job_id'][$i];
$email = $_POST['email'][$i];
$insertQuery = "UPDATE application SET Email_checked = '$email' WHERE Candidate_id = $candidate_id AND Job_id = $job_id";
$inserted = mysqli_query($link, $insertQuery) or die(mysqli_error($link));
}
if($inserted)
{
$message = 'Application successfully update <br>Manage Candidate';;
}
else
{
$message = 'Application failed <br>Manage Candidate';
}
echo $message;
Based on how you described the error in your comments, I would guess that the problem can be solved like this:
$email = isset($_POST['email'][$i]) ? $_POST['email'][$i] : "otherValue";
Replace "otherValue" with whatever you need if it is not set (probably "0")
$email = isset($request->email[$i]) ? $request->email[$i] : 0;
I'm trying to create a system that when i submit the form, after the page refresh it should show the new values that i get from the database. The values work well when they go into the databse but they dont show after submited, only when i refresh again. Thanks for helping
<?php
include("connect.php");
$query = "SELECT * FROM `laliga`";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$home = $row['home'];
$away = $row['away'];
$win = $row['win'];
$draw = $row['draw'];
$lose = $row['lose'];
}
echo "<h2>La Liga</h2>";
echo $home, " - ", $away;
if (isset($_POST) && $_POST['laliga'] == 1){
$select = mysql_real_escape_string($_POST['laliga']);
$select = $win + $select;
mysql_query("UPDATE laliga SET win='$select'");
}else if (isset($_POST) && $_POST['laliga'] == 'X'){
$select = mysql_real_escape_string($_POST['laliga']);
$select = '1';
$select = $draw + $select;
mysql_query("UPDATE laliga SET draw='$select'");
}else if (isset($_POST) && $_POST['laliga'] == 2){
$select = mysql_real_escape_string($_POST['laliga']);
$select = '1';
$select = $lose + $select;
mysql_query("UPDATE laliga SET lose='$select'");
}
header('Location: ../laliga.php');
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="radio" name="laliga" value="1">1
<input type="radio" name="laliga" value="X">X
<input type="radio" name="laliga" value="2">2
<input type="submit" name="submit" value="Submit"/>
</form>
<br/>
<?php
echo $home, " -> ", $win;
echo "<br/>Barazim -> ", $draw,"<br/>";
echo $away, " -> ", $lose;
?>
You should handle all of the post data at the top of the PHP file, whilst the header function will solve your problem it's a silly and inefficient way of approaching it. by handling the post data and updating the database first, by the time you query the database the data is there! at the moment you are trying to find the data and then adding it. does this make sense?
Good luck!
Add:
header('Location: <mypage.php>');
After mysql_query.
I am trying to build admin side of small website which consists of 2 pages: index.php and update php. On index.php I run query, that per-fills html form with data from database, which works fine.
Then I send data via $_POST to update.php page, where I try to get those values into variables and then make an update query. Which fails. I suspect something is wrong with $_POST array - some values are messed up or empty, but I don't understand why.
Here is the code for index.php:
<?php
if (!isset($page_id)) {
echo " <p>Please select page to be edited:</p>";
$query = mysql_query("SELECT page_id, title FROM pages");
$res = mysql_fetch_array($query);
do {
printf("<p><a href='index.php?page_id=%s'>%s</a></p>", $res['page_id'], $res['title']);
} while ($res = mysql_fetch_array($query));
} else { $query = mysql_query("SELECT * FROM pages WHERE page_id = '$page_id'");
$res = mysql_fetch_array($query);
require_once 'parts/form.php';}
?>
This is code for update.php:
<?php
//Here I try to get POST values and assign them to variables for update
//Ths is validation that those values are not empty,
require_once 'parts/guard.php';
if (isset($_POST['page_id'])) {
$page_id = $_POST['page_id'];
}
if (isset($_POST['title'])) {
$title = $_POST['title'];
}
if ($title == '') {
unset($title);
}
if (isset($_POST['description'])) {
$description = $_POST['description'];
}
if ($description == '') {
unset($description);
}
if (isset($_POST['keywords'])) {
$keywords = $_POST['keywords'];
}
if ($keywords == '') {
unset($keywords);
}
if (isset($_POST['text'])) {
$text = $_POST['text'];
}
if ($text == '') {
unset($text);
}
//variables are set
require_once 'parts/meta.php';
?>
<?php
//Here is all the values exist, the query is executed.
//Obviousely this query works in phpmyadmin, but not here - some fields come empty or messed up????
if (isset($title) && isset($keywords) && isset($description) && isset($text) && isset($page_id)) {
$query = mysql_query("UPDATE pages SET title = '$title', description = '$description', keywords = '$keywords', text = '$text' WHERE page_id = '$page_id' ");
if ($query == TRUE) {
echo "<p>Page Updated</p>";
echo "<p><a href = 'http://localhost:8888/travel.ru/admin/index.php'>
Edit Another Page</a></p>";
} else {
echo "<p>Page Is Not Updataed</p>";
}
} else {
echo "<p>You Left Some Fields Empty. Page Will Not Be Updated.</p>";
}
?>
And this is the form I use:
<form name="update" action = "update.php" method= "post">
<p> Page Name<br>
<input value = "<?php echo $res['title']; ?>" type = "text" name = "title"></p>
<p> Page Description<br>
<input value = "<?php echo $res['description']; ?>" type = "text" name = "title"></p>
<p> Page Keywords<br>
<input value = "<?php echo $res['keywords']; ?>" type = "text" name = "title"></p>
<p> Page Content<br>
<textarea type = "text" name ="text" cols = "68" rows = "15"><?php echo $res['text']; ?>
</textarea></p>
<input type = "hidden" name="page_id" value =$res[page_id]>
<p><input type = "submit" name ="submit" value ="Save Changes" id="submit"</p>
</form>
Any help will be most appreciated as I dont have a clue why I have this problem?
Most of your form fields are named title. Thus you don't actually have a field called description or page_id or keywords.
Mate also raises a valid point.
Try added php tag to your input value
<input type = "hidden" name="page_id" value ="<?php echo $res['page_id']; ?>" />
As mentioned Amadan , also check the names for all controls in your form.