How to update information into the database? - php

Im having a problem in updating the database,
This is my first page,
<div class="col-md-4">
<div class="createnewbox">
<form name="Edit Admin Infomation" class="form-horizontal" method="post" action="adminUpdateProductDetail.php?cat=<?php echo $product['categoryid']; ?>&code=<?php echo $product['productname']; ?>">
<h2>Edit Product Information</h2>
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<br/>
<br/>
<div class="col-md-11">
<input type="text" class="form-control" value="<?php echo $product['productname']; ?>" name="productname">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Price</label>
<br/>
<br/>
<div class="col-md-11">
<input type="text" class="form-control" value="<?php echo $product['price']; ?>" name="price">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Dimension</label>
<br/>
<br/>
<div class="col-md-11">
<input type="text" class="form-control" value="<?php echo $product['dimension']; ?>" name="dimension">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Description</label>
<br/>
<br/>
<div class="col-md-11">
<textarea type="text" class="form-control" rows="8" name="productinfo">
<?php echo $product[ 'productinfo']; ?>
</textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-11">
<input type="submit" value="Update Information" class="btn btn-success">
<script>
function reset() {
location.reload();
}
</script>
<button class="btn btn-info" onclick="reset()">undo</button>
</div>
</div>
<?php ?>
</form>
</div>
</div>
This is my adminUpdateProductDetail.php,
<?php
include 'adminNavBar.php';
require 'dbfunction.php';
$con = getDbConnect();
$price = $_POST['price'];
$name = $_POST['productname'];
$info = $_POST['productinfo'];
$dimension = $_POST['dimension'];
$cat= $_GET['cat'];
$code= $_GET['code'];
?>
<div class="space">
<div class="container">
<div class="row">
<?php
if (!mysqli_connect_errno($con)) {
$sqlQueryStr = "UPDATE product SET price = '$price', productname = '$name', productinfo = '$info', dimension = '$dimension' WHERE categoryid = '$cat' AND productname = '$code'";
if (mysqli_query($con, $sqlQueryStr)) {
$recordid = mysqli_insert_id($con);
mysqli_query($con, $sqlQueryStr);
}
mysqli_close($con);
echo "$name Product details updated.";
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
</div>
</div>
</div>
I gotten no error message, the system echo out success. But my database was not updated. I not sure where when wrong.

Try this
<?php
if (!mysqli_connect_errno($con)) {
$sqlQueryStr = "UPDATE product SET price = '$price', productname = '$name', productinfo = '$info', dimension = '$dimension' WHERE categoryid = '$cat' AND productname = '$code'";
if (mysqli_query($con, $sqlQueryStr)) {
$result = mysqli_query($con, $sqlQueryStr);
if($result === FALSE){
printf("Erreur : %s\n", mysqli_error($link));
}
$recordid = mysqli_insert_id($con);
}
mysqli_close($con);
echo "$name Product details updated.";
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
</div>
The doc to help you: http://php.net/manual/en/mysqli.query.php#example-1766 procedural style
mysqli_insert_id always after the mysqli_query update request.
No error with the connection but can be with the query

Related

update not working without any error i am using php and sql

I want to update my form using PHP and SQL but update query not working. select query working but update not working. there is no error. when I am using a select query for show value in the input field and it's working. I am trying much think but not figure out what is the error. I am trying much think but not figure out what is the error.
<?php
include 'connection.php';
include 'config.php';
if (isset($_GET['edit'])) {
{
$topHeading=$_POST['topHeading'];
$mainHeading=$_POST['mainHeading'];
$bottomHeading=$_POST['bottomHeading'];
$filesname = $_FILES['file']['name'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif","svg");
// Check extension
if( in_array($imageFileType,$extensions_arr) ){
// Convert to base64
$image_base64 = base64_encode(file_get_contents($_FILES['file']['tmp_name']) );
$images = 'data:image/'.$imageFileType.';base64,'.$image_base64;
$sql = "update banner set topHeading='$topHeading', filesname='$filesname', images='$images', mainHeading='$mainHeading', bottomHeading='$bottomHeading' where id='$id'";
if (mysqli_query($connection, $sql)) {
echo "Done";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
// mysqli_close($connection);
// Upload file
move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$filesname);
if($sql)
{
echo "";
}
else
{
echo "<script>alert('Data not inserted');</script>";
}
}
}
}
$query = mysqli_query($connection, "select * from banner");
while ($row = mysqli_fetch_array($query)) {
echo "<b><a href='editbanner.php?id={$row['id']}'>{$row['topHeading']}</a></b>";
echo "<br />";
}
if (isset($_GET['edit'])) {
echo '<div class="form" id="form3"><br><br><br><br><br><br>
<Span>Data Updated Successfuly......!!</span></div>';
}
?>
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$count = 0;
$mysqli = mysqli_query($connection,"SELECT * FROM banner");
foreach($mysqli as $row){
$count++;
?>
<form class="form-horizontal" method="post" name="form1" id="form1" enctype="multipart/form-data">
<div class="form-group label-floating form-rose">
<label for="brandname" class="control-label col-xs-2">Brand Name of Stock Broker</label>
<div class="col-xs-12">
<input type="text" name="topHeading" id="brandname" class="form-control" value="<?php echo $row["topHeading"]; ?>"/>
<span class="error" id="pointfn"></span>
</div>
</div>
<div class="form-group label-floating form-rose">
<label for="name" class="control-label col-xs-2">Contact Person</label>
<div class="col-xs-12">
<input type="text" name="mainHeading" id="name" class="form-control" value="<?php echo $row["mainHeading"]; ?>"/>
<span class="error" id="pointln"></span>
</div>
</div>
<div class="form-group label-floating form-rose">
<label for="designation" class="control-label col-xs-2">Designation</label>
<div class="col-xs-12">
<input type="text" name="bottomHeading" id="designation" class="form-control" value="<?php echo $row["bottomHeading"]; ?>"/>
<span class="error" id="designationln"></span>
</div>
</div>
<div class="label-floating form-rose file-upload">
<div class="file-select">
<div class="file-select-button" id="fileName">Upload Logo</div>
<div class="file-select-name" id="noFile">No file chosen...</div>
<input type="file" name="file" id="chooseFile" required>
</div>
</div>
<div class="form-group label-floating form-rose">
<div class="col-xs-12"> <input type='checkbox' name='checkbox' id="checkbox" required /> <label for="checkbox" class="col-xs-10 term">I accept the terms and condition</label>
</div>
</div>
<button type="submit" name="edit" value="Submit" id="formT" class="button-cta cta btn" onClick="return validateForm()"> I am Interested!</button>
</form>
<?php }} ?>
</body>
</html>
$id is missing above your update query...
$id = $_REQUEST['id'];
$sql = "update banner set topHeading='$topHeading', filesname='$filesname', images='$images', mainHeading='$mainHeading', bottomHeading='$bottomHeading' where id='$id'";

Getting the same data for all the records for editing and updating a record in php

I am having two different database tables questions and choices where i am inserting questions in one table and multiple choices in another table where questions table id is foreign key in choices table.
Questions:
Questions_number Text
1 What is HTML?
2 What is PHP?
Choices:
id question_number is_correct text
1 1 1 markup
2 1 0 Hyext
3 1 0 Hyper text markup language
4 2 0 hsdfd
5 2 0 frfwer
6 2 1 Hypertext Preprocessor
If i am trying to edit question number 1 then i need to fetch all the details of questions,Choices and correct option as well.But when i am trying to edit the record for choices as well i am getting the same data which i am getting for question.
HTML:
<?php session_start();
include 'includes/db.php';
$id = (int)$_GET['id'];
$sql = "SELECT * FROM questions q WHERE q.question_number = $id ";
$oppointArr =array();
$result = mysqli_query($mysqli,$sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result))
{
$oppointArr = $row;
echo "Text: " . $row["text"]. "<br>";
}
} else {
echo "0 results";
}
?>
<form class="form-horizontal" action="updatequestions.php" method="post" role="form">
<?php if(isset($msg)) {?>
<div class="<?php echo $msgclass; ?>" id="mydiv" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<input type='hidden' value='<?=$id;?>' name='question_number'>
<h2>Edit A Question</h1>
<div class="form-group">
<label for="questionno" class="col-sm-2 control-label">Question Number</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['question_number'];?>"
name="question_number" id="question_number" readonly>
</div>
</div>
<div class="form-group">
<label for="question" class="col-sm-2 control-label">Question</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['text'];?>" name="question_text" id="question_text">
</div>
</div>
<input type='hidden' value='<?=$id;?>' name='id'>
<h2>Edit A Choice</h1>
<div class="form-group">
<label for="choice #1" class="col-sm-2 control-label">Choice #1</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['choice1'];?>" name="choice1" id="choice1">
</div>
</div>
<div class="form-group">
<label for="choice #2" class="col-sm-2 control-label">Choice #2</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['choice2'];?>" name="choice2" id="choice2">
</div>
</div>
<div class="form-group">
<label for="choice #3" class="col-sm-2 control-label">Choice #3</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['choice3'];?>" name="choice3" id="choice3">
</div>
</div>
<div class="form-group">
<label for="Correct Choice Number:" class="col-sm-2 control-label">Correct Choice Number:</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['is_correct'];?>" name="is_correct" id="is_correct">
</div>
</div>
<div class="col-sm-offset-2">
<button type="submit" class="btn btn-default" name="submit_user" id="subject">Submit</button>
<button type="cancel" class="btn btn-raised">Cancel</button>
</div>
</form>
Updatequestions:
<?php
include 'includes/db.php';
if(isset($_POST['submit_user']))
{
$questiontext = $_POST['question_text'];
$id=$_POST['question_number'];
$correct_choice = $_POST['correct_choice'];
$choices = array();
$choices[1] = $_POST['choice1'];
$choices[2] = $_POST['choice2'];
$choices[3] = $_POST['choice3'];
$choices[4] = $_POST['choice4'];
$choices[5] = $_POST['choice5'];
$query = "UPDATE questions SET text='$questiontext' WHERE question_number = $id";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row) {
foreach($choices as $choice => $value){
if($value != ''){
if($correct_choice == $choice){
$is_correct = 1;
} else {
$is_correct = 0;
}
$query = "UPDATE choices SET is_correct='$is_correct', text='$value' WHERE question_number=$id";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row){
continue;
} else {
die('Error : ('.$mysqli->errno . ') '. $mysqli->error);
}
}
}
$msg = 'Question has been added';
}
}
?>
If i try to update the record all the fields are updating with the same data.
WARNING: Do not create SQL statements by concatenating the data with SQL. Use prepared statements.
As for your problem, you use the foreign key of the question to update choices. The key is not the primary key of choices and is not unique. Try using the unique primary key for your SQL.
Instead of this:
$query = "UPDATE choices SET is_correct='$is_correct', text='$value' WHERE question_number=$id";
try this:
$query = "UPDATE choices SET is_correct='$is_correct', text='$value' WHERE id=$choice ";
But of course you should really try to do it all over again using prepared statements instead!
<form class="form-horizontal" action="updatequestions.php" method="post" role="form">
<?php if(isset($msg)) {?>
<div class="<?php echo $msgclass; ?>" id="mydiv" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<input type='hidden' value='<?=$id;?>' name='question_number'>
<h2>Edit A Question</h1>
<div class="form-group">
<label for="questionno" class="col-sm-2 control-label">Question Number</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['question_number'];?>"
name="question_number" id="question_number" readonly>
</div>
</div>
<div class="form-group">
<label for="question" class="col-sm-2 control-label">Question</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $oppointArr['text'];?>" name="question_text" id="question_text">
</div>
</div>
<input type='hidden' value='<?=$id;?>' name='id'>
<h2>Edit A Choice</h1>
<?php
$choicesql = "SELECT * FROM `choices` WHERE question_number = $id ";
$ChoicetArr =array();
$choiceresult = mysqli_query($mysqli,$choicesql);
$inc=1;
$correctAns ="";
if (mysqli_num_rows($choiceresult) > 0)
{
while($rows = mysqli_fetch_array($choiceresult))
{
$ChoicetArr[] = $rows;
?>
<div class="form-group">
<label for="choice #<?php echo $inc;?>" class="col-sm-2 control-label">Choice #<?php echo $inc;?></label>
<div class="col-sm-5">
<input type="hidden" name="choice_id<?php echo $inc;?>" value="<?php echo $rows['id'];?>">
<input type="text" class="form-control" value="<?php echo $rows['text'];?>" name="choice<?php echo $inc;?>" id="choice<?php echo $inc;?>">
</div>
</div>
<?php
//print_r($rows);
if($rows['is_correct']=="1"){
$correctAns = '<input type="hidden" name="choice_id'.$inc.'" value="'.$rows['id'].'"><div class="form-group">
<label for="Correct Choice Number:" class="col-sm-2 control-label">Correct Choice Number:</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="'.$inc.'" name="is_correct" id="is_correct">
</div>
</div>';
}
$inc++;
}
}
echo $correctAns;
?>
<div class="col-sm-offset-2">
<button type="submit" class="btn btn-default" name="submit_user" id="subject">Submit</button>
<button type="cancel" class="btn btn-raised">Cancel</button>
</div>
</form>
updatequestions.php
<?php
include 'includes/db.php';
if(isset($_POST['submit_user']))
{
$questiontext = $_POST['question_text'];
$id=$_POST['question_number'];
$correct_choice = $_POST['is_correct'];
$choices = array();
$choices[] = array("question"=>$_POST['choice1'], "answer"=>$_POST['choice_id1']);
$choices[] = array("question"=>$_POST['choice2'], "answer"=>$_POST['choice_id2']);
$choices[] = array("question"=>$_POST['choice3'], "answer"=>$_POST['choice_id3']);
$choices[] = array("question"=>$_POST['choice4'], "answer"=>$_POST['choice_id4']);
$choices[] = array("question"=>$_POST['choice5'], "answer"=>$_POST['choice_id5']);
$query = "UPDATE questions SET text='$questiontext' WHERE question_number = $id";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row)
{
$inc= 0;
foreach($choices as $choice => $value){
if(count($value)>0){
$answerInc = $choice+1;
if($correct_choice == $answerInc){
$is_correct = 1;
} else {
$is_correct = 0;
}
$text= $value['question'];
$answer = $value['answer'];
//echo "<br>".$text;
//print_r($value);
echo $answerInc;
echo "<br>";
echo $query = "UPDATE choices SET is_correct='$is_correct', text='$text' WHERE id=$answer";
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($insert_row){
continue;
} else {
die('Error : ('.$mysqli->errno . ') '. $mysqli->error);
}
}
$inc++;
}
$msg = 'Question has been Updated Successfully';
header("location:searchquestions.php");
exit;
}
}
?>

Update query is not working in Mysql but instead inserting new data in this query

Table name = image_2017.My image is not updating by this UPDATE query in a particular $_GET['id'] instead image is inserting as a new image. I don't know what is wrong in this code.I am new to php. Kindly help me someone.
<?php
include_once "config.php";
session_start();
if(!isset($_SESSION)){
header("index.php");
}
$id = $_GET['id'];
//echo $id;
if(!empty($_FILES)){
$t = time();
$filename = $category."_".$t."_".$_FILES['image']['name'];
$upload = "uploads/";
$fileupload = move_uploaded_file($_FILES['image']['tmp_name'],$upload.$filename);
if($fileupload){
$msg1 = "File uploaded Successfully";
}else{
$msg2 = "File uploaded Failed";
}
}
if(!empty($_POST)){
$category = $_POST['category'];
$image = $_FILES['image'];
$query = "UPDATE image_2017 SET category ='$category', image ='$filename' WHERE id ='$id' ";
$result = $db->query($query);
if($result){
$msg3 = "Image Updated Successfully";
}else{
$msg4 = "Image not Updated";
}
}else{
//echo "Please enter all the details";
}
?>
<html>
<body>
<form class="form news" style="padding:10px;" method="post" action="image-2017.php" enctype="multipart/form-data">
<div class="row">
<div class="form-group">
<label class="control-label col-md-2">category</label>
<div class="col-md-4">
<select class="form-control" name="category">
<option>--> Select <--</option>
<option>Birthday</option>
<option>Christmas</option>
<option>Fruits</option>
<option>Ganesh Chathurthi</option>
<option>Green Day</option>
<option>Guitar Play</option>
<option>Independence Day</option>
<option>Krishna Jayanthi</option>
<option>Onam</option>
<option>Splash Pool</option>
<option>Teddy Bear</option>
<option>Veg Market</option>
<option>Vijayadhasami</option>
</select>
</div>
</div>
</div>
<br>
<div class="row">
<div class="form-group">
<label class="control-label col-md-2">Upload Image</label>
<div class="col-md-10">
<input type="file" name="image">
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2"></label>
<div class="col-md-10">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-primary">Back</button>
</div>
</div>
</form>
</body>
</html>
I updated html to add a post for id
<?php
include_once "config.php";
session_start();
if(!isset($_SESSION)){
header("index.php");
}
$id = $_POST['id'];
//echo $id;
if(!empty($_FILES)){
$t = time();
$filename = $category."_".$t."_".$_FILES['image']['name'];
$upload = "uploads/";
$fileupload = move_uploaded_file($_FILES['image']['tmp_name'],$upload.$filename);
if($fileupload){
$msg1 = "File uploaded Successfully";
}else{
$msg2 = "File uploaded Failed";
}
}
if(!empty($_POST)){
$category = $_POST['category'];
$image = $_FILES['image'];
$query = "UPDATE image_2017 SET category ='$category', image ='$filename' WHERE id ='$id' ";
$result = $db->query($query);
if($result){
$msg3 = "Image Updated Successfully";
}else{
$msg4 = "Image not Updated";
}
}else{
//echo "Please enter all the details";
}
?>
<html>
<body>
<form class="form news" style="padding:10px;" method="post" action="image-2017.php" enctype="multipart/form-data">
<div class="row">
<div class="form-group">
<label class="control-label col-md-2">category</label>
<div class="col-md-4">
<select class="form-control" name="category">
<option>--> Select <--</option>
<option>Birthday</option>
<option>Christmas</option>
<option>Fruits</option>
<option>Ganesh Chathurthi</option>
<option>Green Day</option>
<option>Guitar Play</option>
<option>Independence Day</option>
<option>Krishna Jayanthi</option>
<option>Onam</option>
<option>Splash Pool</option>
<option>Teddy Bear</option>
<option>Veg Market</option>
<option>Vijayadhasami</option>
</select>
</div>
</div>
</div>
<br>
<div class="row">
<div class="form-group">
<label class="control-label col-md-2">Upload Image</label>
<div class="col-md-10">
<input type="file" name="image">
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2"></label>
<div class="col-md-10">
<input type="hidden" name="id" value="<?php echo $_GET['id'];?>">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-primary">Back</button>
</div>
</div>
</form>
</body>
</html>
Make sure you reach the page as: mypage.php?id=12

Submit button won't work in PHP

PHP Code
<?php
if (!isset($_SESSION)) { session_start(); }
include "connect.php";
include "functions.php";
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
header('location: no_acces.php');
exit();
} else {
$id_user = $_SESSION['userid'];
$q_user = mysqli_query($conn, "SELECT * FROM users WHERE id = $id_user");
if (mysqli_num_rows($q_user) === 1) {
$r_user = mysqli_fetch_assoc($q_user);
} else {
unset($_SESSION['login']);
unset($_SESSION['userid']);
header('location: no_acces.php');
exit();
}
}
$error = "";
$userQuery = mysqli_query($conn, "SELECT username FROM users");
$user = mysqli_fetch_assoc($userQuery);
$id = $_GET['id'];
if (isset($_POST['edit_contact'])) {
$roepnaam = $_POST['roepnaam'];
$naam = $_POST['naam'];
$land = $_POST['land'];
$bedrijf = $_POST['bedrijf'];
$adres1 = $_POST['adres1'];
$adres2 = $_POST['adres2'];
$stad = $_POST['stad'];
$postcode = $_POST['postcode'];
$provincie = $_POST['provincie'];
$telefoon = $_POST['telefoon'];
$email = $_POST['email'];
$captcha= $_POST['g-recaptcha-response'];
if(!$captcha){
$error = "Er is een fout opgetreden";
}
if ($error == "") {
$insertUser = ("UPDATE address SET
roepnaam = '$roepnaam', naam = '$naam', bedrijf = '$bedrijf', telefoon = '$telefoon', email = '$email', adres1 = '$adres1', adres2 = '$adres2', stad = '$stad', postcode = '$postcode', provincie = '$provincie', land = '$land' WHERE id = $id");
if (mysqli_query($conn, $insertUser)) {
$_SESSION['edit_contact'] = true;
header('location: address_book.php');
} else {
$error = "Er is een fout opgetreden";
}
}
}
?>
HTML Code
<!DOCTYPE html>
<html lang="en">
<body>
<form action="" method="post">
<?php if ($error !== "") { ?>
<div class="row">
<div class="col-md-12 error">
<?php echo $error; ?>
</div>
</div>
<?php } ?>
<label for="firstName" class="control-label">Naam:</label>
<div class="row ">
<div class="col-md-6">
<input type="text" class="form-control" id="firstName" placeholder="Roepnaam" name="roepnaam" value="<?php if (isset($_POST['roepnaam'])) { echo $_POST['roepnaam']; } ?>" required/>
</div>
<div class="col-md-6">
<input type="text" class="form-control" id="lastName" placeholder="Naam" name="naam" value="<?php if (isset($_POST['naam'])) { echo $_POST['naam']; } ?>" required/>
</div>
</div>
<label for="username" class="control-label">Bedrijf:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="username" placeholder="Bedrijf" name="bedrijf" value="<?php if (isset($_POST['bedrijf'])) { echo $_POST['bedrijf']; } ?>" required/>
</div>
</div>
<label for="password" class="control-label">Telefoonnummer:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="password" placeholder="Telefoonnummer" name="telefoon" value="<?php if (isset($_POST['telefoon'])) { echo $_POST['telefoon']; } ?>" required/>
</div>
</div>
<label for="email" class="control-label">Email:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="email" placeholder="E-mailadres" name="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" required/>
</div>
</div>
<label for="adres1" class="control-label">Adres:</label>
<div class="row">
<div class="col-md-12">
<input type="text" class="form-control" id="adres1" placeholder="Adres 1" name="adres1" value="<?php if (isset($_POST['adres1'])) { echo $_POST['adres1']; } ?>" required/>
</div>
</div>
<div class="row padding-top-10">
<div class="col-md-12">
<input type="text" class="form-control" id="adres2" placeholder="Adres 2" name="adres2" value="<?php if (isset($_POST['adres2'])) { echo $_POST['adres2']; } ?>"/>
</div>
</div>
<div class="row">
<div class="col-md-3">
<label for="postcode" class="control-label">Postcode:</label>
</div>
<div class="col-md-5">
<label for="city" class="control-label">Stad:</label>
</div>
<div class="col-md-4">
<label for="regio" class="control-label">Regio:</label>
</div>
</div>
<div class="row ">
<div class="col-md-3">
<input type="text" class="form-control" id="postcode" placeholder="Postcode" name="postcode" value="<?php if (isset($_POST['postcode'])) { echo $_POST['postcode']; } ?>" required/>
</div>
<div class="col-md-5">
<input type="text" class="form-control" id="city" placeholder="Stad" name="stad" value="<?php if (isset($_POST['stad'])) { echo $_POST['stad']; } ?>" required/>
</div>
<div class="col-md-4">
<input type="text" class="form-control" id="regio" placeholder="Provincie" name="provincie" value="<?php if (isset($_POST['provincie'])) { echo $_POST['provincie']; } ?>" required/>
</div>
</div>
<label for="land" class="control-label">Land:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="password" placeholder="Land" name="land" value="<?php if (isset($_POST['land'])) { echo $_POST['land']; } ?>" required/>
</div>
</div>
<div class="row">
<div class="col-md-8 padding-top-10 ">
<div class="g-recaptcha " data-sitekey="6LcCsBoTAAAAAK72uzyJSrgWwD8xuF6jFIfgFaHX"></div>
</div>
</div>
<div class="row">
<div class="col-md-2 padding-top-10">
<input type="submit" name="edit_contact" class="btn btn-succes" value="Wijzigen">
</div>
<div class="col-md-2 padding-top-10">
<input type="text" name="delete_contact" action="delete_contact.php" class="btn btn-succes" value="Contact verwijderen">
</div>
</div>
</form>
</body>
</html>
PHP Code
<?php
if (!isset($_SESSION)) { session_start(); }
include "connect.php";
include "functions.php";
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true || !isset($_SESSION['userid']) || $_SESSION['userid'] == "") {
header('location: login.php');
exit();
} else {
session_regenerate_id();
}
$id = $_GET['id'];
$query = "DELETE FROM address WHERE id= $id";
mysqli_query ($query);
if (mysql_affected_rows() == 1) {
header('location: addressbook.php');
} else {
echo "Verwijderen mislukt";
}
?>
I'm trying to make a delete button for my contacts within the addressbook. but everytime I click "Contact verwijderen" the webpage resets it self and the contact won't be deleted. Could anyone help me to fix this?
You input is a text input and you don't have a form asociated with it,create one and change the type of submit to submit
<form action="delete_contact.php" method="post">
//other inputs
<input type="submit" name="delete_contact" class="btn btn-succes" value="Contact verwijderen">
</form>
You are mixing MySQL and MySQLi functions:
mysqli_query ($query);
if (mysql_affected_rows() == 1)
You cannot mix MySQL with MySQLi, your code should be:
mysqli_query ($query);
if (mysqli_affected_rows($conn) == 1)
Add a normal link to delete the contact, you don't need a form.
<a href="delete_contact.php?id=<?php echo $id ?>">
Contact verwijderen
</a>

Why isn't PHP sending POST request?

I try to submit a form, but it does not send my form. If I try to print the POST array its simply empty.
Here is my form:
<?php
print_r($_POST);
require('includes/config.php');
require('includes/auth_admin.php');
$title = "New Block Day";
require ('includes/header.php');
//if form has been submitted process it
if(isset($_POST['submit'])){
if(empty($_POST['name'])){
$error[] = 'Please Enter a name.';
}
$date = $_POST['date'];
$result = mysqli_query($con, "select name from official_holiday where date = $date");
while ($row = mysqli_fetch_assoc($result)) {
$check[] = $row;
}
if(empty($_POST['date'])){
$error[] = 'Please Enter a date.';
}elseif(!empty($_POST['date'])){
$error[] = 'Already block date on that day.';
}
$new_date = $_POST['date'];
$new_name = $_POST['name'];
$query = sprintf("INSERT INTO official_holiday (name, date) VALUES ('%s', '%s')",
mysql_real_escape_string($new_name),
mysql_real_escape_string($new_date));
$result = mysqli_query($con, $query);
header("Location:new_block.php?id=$id&changes=successful");
}else{
//header('0');
//exit;
}
?>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
<form action="new_block.php" method="post" role="form" >
<h3><?php echo $title ?></h3>
<p><a href='block_days.php'>Back</a></p>
<?php
if(isset($_GET['changes']) && $_GET['changes'] = 'successful'){
echo '<h4>Changes successful.</h4>';
}
?>
<hr>
<label for="name">Name</label>
<div class="form-group">
<input type="text" required name="name" id="name" class="form-control" value="<?php if(isset($error)){ echo $_POST['name']; } ?>" tabindex="3">
</div>
<div class="form-group">
<label for="datepicker">Date</label>
<div class="input-group input-append date">
<input type="text" class="form-control" id="datepicker" name="date" placeholder="Date" value="<?php if(isset($error)){ echo $_POST['date']; } ?>">
<span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar">
</div>
</div>
<div class="form-group">
<input type="submit" name="submit" id="name" value="submit" class="btn btn-info btn-block" tabindex="5">
</div>
</form>
</div>
</div>
</div>
<?php
require('includes/footer.php');
?>
Why does PHP not send anything?
EDIT: I added the full code, but there's not much I can add to this.
Please comment your redirect first:
// header("Location:..")

Categories