As i am newbie to PHP kindly pardon me if i looks silly ,
I created a form in php , while i do the update part of the form the update reflects in db whereas in the form it still shows the same old value . i tried refresh and force refresh but nothing changes .
Whereas if i logout and login again , the form shows the updated value .
I tried using die(); after mysql_close($link); but it logs out the session and needs to re-login .
Kindly help me on viewing the changes while i am still inside the login .
My code is as follows :
<?php
if(isset($_POST['update'])) {
$name_a = $_POST['name'];
$email_a = $_POST['email'];
$pass_a = $_POST['password'];
$sql = "UPDATE admin SET a_name = '$name_a', a_email = '$email_a', password = '$pass_a' where aid='$update_id' ";
$retval = mysql_query($sql,$link);
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($link);
}else {
?>
<!-- Widget: user widget style 1 -->
<div class="box box-widget widget-user-2">
<!-- Add the bg color to the header using any of the bg-* classes -->
<div class="widget-user-header bg-yellow">
<div class="widget-user-image">
<?php echo '<img src="' . $img . '" class="img-circle" alt="User Image">'; ?>
</div>
<!-- /.widget-user-image -->
<h3 class="widget-user-username"><?php echo "$name"; ?></h3>
<h5 class="widget-user-desc"><?php echo "$role"; ?></h5>
</div>
<div class="box-footer no-padding">
<form role="form" method = "post" action = "<?php $_PHP_SELF ?>">
<div class="box-body">
<div class="form-group">
<label for="exampleInputName1">Name</label>
<input type="text" class="form-control" id="exampleInputName1" name="name" value="<?php echo "$name"; ?>">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="email" value="<?php echo "$email"; ?>">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password" value="<?php echo "$password"; ?>">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="update" id="update" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
<!-- /.widget-user -->
<?php
}
?>
SOLUTION
1) use the updated value like $name_a instead of $name because $name_a contain updated value and $name contain old value
2) reload page after update and get new value from database on page load and store that value in $name , $email etc variable (if new data update successfully in database then only you get new value )
3) if You store your data in session or cookie then update session and cookie value also when you update in database
Try this:
<?php
$name = '';
$email = '';
$password = '';
$update_id = '';
//$img = '';
//$role = '';
//$link = null;
if(
isset($_POST['update']) &&
isset($_POST['id']) &&
isset($_POST['name']) &&
isset($_POST['email']) &&
isset($_POST['password'])
) {
$update_id = mysql_real_escape_string($_POST['id']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$sql = 'UPDATE admin SET a_name = \'' . $name . '\', a_email = \'' . $email . '\', password = \'' . $password . '\' WHERE aid = \'' . $update_id . '\'';
$result = #mysql_query($sql, $link);
if(!$result)
die('Could not update data: ' . mysql_error($link));
echo 'Updated data successfully', "\n";
}
elseif(isset($_GET['id'][0])) {
$update_id = mysql_real_escape_string($_GET['id']);
$sql = 'SELECT a_name,a_email,a_password FROM admin WHERE aid = \'' . $update_id . '\'';
$result = #mysql_query($sql, $link);
if($result) {
$result = mysql_fetch_row($result);
$name = $result[0];
$email = $result[1];
$password = $result[2];
}
else {
echo 'Could not find the id.' . "\n";
$update_id = '';
}
}
unset($result);
if(isset($update_id[0])) {
mysql_close($link);
?>
<!-- Widget: user widget style 1 -->
<div class="box box-widget widget-user-2">
<!-- Add the bg color to the header using any of the bg-* classes -->
<div class="widget-user-header bg-yellow">
<div class="widget-user-image">
<img src="<?php echo htmlspecialchars($img); ?>" class="img-circle" alt="User Image">
</div>
<!-- /.widget-user-image -->
<h3 class="widget-user-username"><?php echo htmlspecialchars($name); ?></h3>
<h5 class="widget-user-desc"><?php echo htmlspecialchars($role); ?></h5>
</div>
<div class="box-footer no-padding">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($update_id); ?>">
<div class="box-body">
<div class="form-group">
<label for="exampleInputName1">Name</label>
<input type="text" class="form-control" id="exampleInputName1" name="name" value="<?php echo htmlspecialchars($name); ?>">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="email" value="<?php echo htmlspecialchars($email); ?>">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password" value="<?php echo htmlspecialchars($password); ?>">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="update" id="update" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
<!-- /.widget-user -->
<?php }
else {
$sql = 'SELECT aid,a_name FROM admin';
$result = #mysql_query($sql, $link);
if($result) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '' . $row['a_name'] . '<br />' . "\n";
}
}
mysql_close($link);
}
?>
As #DivyeshSavaliya mentioned in the comment the issue is ,
I didn't Used Select query after update . Once done that the issue solved
The new working code is
<?php
if(isset($_POST['update'])) {
$name_a = $_POST['name'];
$email_a = $_POST['email'];
$pass_a = $_POST['password'];
$sql = "UPDATE admin SET a_name = '$name_a', a_email = '$email_a', password = '$pass_a' where aid='$update_id' ";
$retval = mysql_query($sql,$link);
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
}
$result = mysql_query("SELECT * FROM admin where aid='$update_id' ",$link);
while($row = mysql_fetch_array($result)){
$name = $row['a_name'];
$email = $row['a_email'];
$password = $row['password'];
}
mysql_close($link);
?>
Thanks to #DivyeshSavaliya
Related
I have a simple test.php script:
<?php
$name = $_GET['name'];
$response = "Hi, ".$name."! How are you?";
echo $response;
?>
I know I can echo the html script in the php like: echo "<b>".$response."</b>"; but how can I just return the $response to my html page, and in the html page, it format the $response string?
I have a index.html page:
<title>Your name</title>
<center>Welcome to the coolest page ever</center>
<br>
"The PHP $response goes here"
<br>
<u>This is the footer</u>
As you can see, there is a html page with a template, and the response is required on one spot only (in the middle of the page). I don't want to echo the entire html script.
If I put the php script into the html, then my php script is exposed...
Example link: www.domain.com/test.php?name=John
save this page with whatever.php.
<html>
<title>Your name</title>
<body>
<center>Welcome to the coolest page ever</center>
<br>
<?php if(isset($_GET['name'])) { $response = "Hi, ".$_GET['name']."! How are you?";
echo $response;}
?>
<br>
<u>This is the footer</u>
</body></html>
You can put the html in your php page test.php
Here is an example of how I have used echo inside of html to display a value
First do your php to connect to your database and execute whatever query you want
I use includes as well to make dealing with the header/Navigation and footer simple for every page. Instead of having to have that code inside of your test.php page you can create separate pages and just include them. That way if you want to edit your header or footer you can do it in one place and it will change for all of your pages.
As you will see in my html you can then put the echo value into the html like so value="
//php code begins here
<?php
include_once 'header.php';
$sql = "SELECT * FROM `person` WHERE person_id = " .$user_id." ";
$conn = mysqli_connect( $dbServername , $dbUsername , $dbPassword , $dbName );
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "user_id: " . $row["person_id"]. " - person_id: " . $row["person_first"]. " " . $row["person_last"]. "<br>";
$person_id = $row["person_id"];
$person_first = $row["person_first"];
}
} else {
echo $sql;
}
$conn->close();
?>
//first php section ends here
//html section starts here
<div class="form-group">
<label class="col-md-12">First Name</label>
<div class="col-md-12">
<input type="text" required="" name="person_first" value="<?php echo $person_first;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">Last Name</label>
<div class="col-md-12">
<input type="text" required="" name="person_last" value="<?php echo $person_last;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label for="example-email" class="col-md-12">Mobile</label>
<div class="col-md-12">
<input type="text" required="" name="person_mobile" value="<?php echo $person_mobile;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label for="example-email" class="col-md-12">Address</label>
<div class="col-md-12">
<input type="address" required="" name="person_address" value="<?php echo $person_address;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">Email</label>
<div class="col-md-12">
<input type="text" required="" name="person_email" value="<?php echo $person_email;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">SSN</label>
<div class="col-md-12">
<input type="text" required="" name="SSN" value="<?php echo $SSN;?>" class="form-control form-control-line"> </div>
</div>
//html ends here
//php footer code starts here
<?php
include_once 'footer.php';
?>
// php footer ends here
Begin code for new page header.php
<?php
include 'includes/dbh.php';
session_start();
if (!isset($_SESSION["u_id"]))
{
header("location: login.php");
}
$user_id = $_SESSION['u_id'];
echo "UserID is '".$user_id."'";
$sql = "SELECT * FROM users WHERE user_id='$user_id'";
// $sql = "SELECT SINGLE `user_id` FROM `users` WHERE user_id = '".$user_id."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "user_id: " . $row["user_id"]. " - Name: " . $row["user_first"]. " " . $row["user_last"]. "<br>";
$displayname = $row["user_first"]. " " . $row["user_last"];
$email = $row["user_email"];
$uid = $row["user_uid"];
}
} else {
echo "0 results";
}
$sql = "SELECT * FROM players WHERE player_id=$user_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "player_id: " . $row["player_id"]. " - player_id: " . $row["player_first"]. " " . $row["player_last"]. "<br>";
$player_id = $row["player_id"];
$player_first = $row["player_first"];
$player_last = $row["player_last"];
$player_mobile = $row["mobile"];
$player_address = $row["player_address"];
$player_city = $row["city"];
$player_state = $row["state"];
$player_zip = $row["zip"];
$player_dob = $row["dob"];
$player_gender = $row["gender"];
}
}
$conn->close();
?>
End header.php
i had an error when i run my code and i don't understand this error
error:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE UserID = 'ahmed' SET Username = 'adasda#dmail.ck',Email = 'ahmed',FullName' at line 1 in C:\wamp64\www\eC
<?php
/*
==============================================================
= Manage Member do
= you can Add | Edit | Delete Members from here
==============================================================
*/
session_start();
$pageTitle = 'Members';
if(isset($_SESSION['Username'])){
include 'init.php';
$do = isset($_GET['do']) ? $_GET['do'] : 'Manage';
// $do= '';
//
// if(isset($_GET['do'])){
//
// $do = $_GET['do'];
// }else {
// $do = 'Manage';
// }
// start Manage do
if ($do == 'Manage') {
echo 'welcom in manage do';
//Manage page
}elseif ($do == 'Edit') { //edit page
// check If the GET Request is Numeric && Get the Integer value of it
$userid = isset($_GET['userid']) && ($_GET['userid']) ? intval($_GET['userid']) : 0;
// Select the row of user from the table
// select All data Depend on this Id
$stmt = $con->prepare("SELECT * FROM users WHERE UserID = ? LIMIT 1");
// extract Query
$stmt->execute(array($userid));
// Fetch the data
$row = $stmt->fetch();
// the row count
$count = $stmt->rowCount(); // to count the row in the table
if ($stmt->rowCount() > 0) {
?>
<h1 class="text-center">Edit Member</h1>
<div class="container">
<form class="form-horizontal" action="?do=Update" method="POST">
<input type="hidden" name='userid' value="<?php echo $userid ?>"/>
<div class="form-group form-group-lg">
<label class="col-sm-2 control-lable">Username</label>
<div class="col-sm-10">
<input type="text" name="username" class="form-control" value="<?php echo $row['Username'] ?>" autocomplete="off"/>
</div>
</div>
<div class="form-group form-group-lg">
<label class="col-sm-2 control-lable">Password</label>
<div class="col-sm-10">
<input type="hidden" name="oldpassword"/>
<input type="password" name="newpassword" class="form-control" autocomplete="new-password"/>
</div>
</div>
<div class="form-group form-group-lg">
<label class="col-sm-2 control-lable">E-mail</label>
<div class="col-sm-10">
<input type="email" name="email" class="form-control" value="<?php echo$row['Email'] ?>" autocomplete="off"/>
</div>
</div>
<div class="form-group form-group-lg">
<label class="col-sm-2 control-lable">Full-Name</label>
<div class="col-sm-10">
<input type="text" name="full" class="form-control" value="<?php echo$row['FullName'] ?>" autocomplete="off" />
</div>
</div>
<div class="form-group form-group-lg">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="save" class="btn btn-primary btn-lg" />
</div>
</div>
</form>
</div>
<?php
}else {
echo "you are not welcom in this page ";
}
}
// update page
elseif ($do == 'Update') {
echo "<h1 class='text-center'> welcom in the update page </h1>";
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
# get the variable from the form
$id = $_POST['userid'];
$user = $_POST['username'];
$email= $_POST['email'];
$name = $_POST['full'];
//echo $id . $user . $pass . $name;
$stmt = $con->prepare('UPDATE users WHERE UserID = ? SET Username = ?,Email = ?,FullName = ?,');
$stmt->execute(array($user,$email,$name,$id));
echo $stmt->rowCount() . "Record Updated";
}else {
echo "you cant brows this page directly";
}
}
include $tpl . 'footer.php';
}else {
header('location: index.php');
exit();
}
?>
ommers\first_project\admin\members.php on line 110
Your update query is incorrect, you need to use the following:
$stmt = $con->prepare('UPDATE users SET Username = ?,Email = ?,FullName = ? Where UserId =?');
And change the rest of the code accordingly.
I'm attempting to add the update function to my CRUD application. Essentially it uses the database specified, and uses the 'id' from the index.php page, which is 'productID' from the database. In another part of the application, a store management feature is included with the same skeleton Update page and works perfectly.
The database (Product) contains productID(PK), productName, productPrice, storeID(FK), productDate, productComments, productQuantity, and productPortion.
I'm certain it's within the PHP script, likely around the UPDATE command after using a few error checks but I can't seem to figure out what might be the main issue.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="span10 offset1">
<div class="row">
<h3>Update an Item</h3>
</div>
<form class="form-horizontal" action="update.php" method="post">
<input type="hidden" name="productID" value="<?php echo $id ?>">
<div class="control-group <?php echo !empty($nameError)?'error':'';?>">
<label class="control-label">Item</label>
<div class="controls">
<input name="productName" type="text" placeholder="Product Name" value="<?php echo !empty($productName)?$productName:'';?>">
<?php if (!empty($nameError)): ?>
<span class="help-inline"><?php echo $nameError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($priceError)?'error':'';?>">
<label class="control-label">Price</label>
<div class="controls">
<input name="productPrice" type="number" step="any" placeholder="Price" value="<?php echo !empty($productPrice)?$productPrice:'';?>">
<?php if (!empty($priceError)): ?>
<span class="help-inline"><?php echo $priceError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($storeError)?'error':'';?>">
<label class="control-label">Store</label>
<div class="controls">
<select name="storeID" class="form-control">
<option value="">Select Store</option>
<?php $pdo=D atabase::connect(); $sql='SELECT * FROM Store ORDER BY storeName DESC' ; foreach ($pdo->query($sql) as $row) { $selected = $row['storeID']==$storeID?'selected':''; echo '
<option value="'. $row['storeID'] .'" '. $selected .'>'. $row['storeName'] .'</option>'; } Database::disconnect(); ?>
</select>
<?php if (!empty($storeError)): ?>
<span class="help-inline"><?php echo $storeError;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($dateError)?'error':'';?>">
<label class="control-label">Date</label>
<div class="controls">
<input name="productDate" type="date" step="any" placeholder="Date" value="<?php echo !empty($productDate)?$productDate:'';?>">
<?php if (!empty($dateError)): ?>
<span class="help-inline"><?php echo $dateError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($commentsError)?'error':'';?>">
<label class="control-label">Comments</label>
<div class="controls">
<input name="productComments" type="text" placeholder="Comments" value="<?php echo !empty($productComments)?$productComments:'';?>">
<?php if (!empty($commentsError)): ?>
<span class="help-inline"><?php echo $commentsError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($quantityError)?'error':'';?>">
<label class="control-label">Quantity</label>
<div class="controls">
<input name="productQuantity" type="number" placeholder="Quantity" value="<?php echo !empty($productQuantity)?$productQuantity:'';?>">
<?php if (!empty($quantityError)): ?>
<span class="help-inline"><?php echo $quantityError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($portionError)?'error':'';?>">
<label class="control-label">Portion</label>
<div class="controls">
<input name="productPortion" type="number" placeholder="Portion" value="<?php echo !empty($productPortion)?$productPortion:'';?>">
<?php if (!empty($portionError)): ?>
<span class="help-inline"><?php echo $portionError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Update</button>
<a class="btn" href="index.php">Back</a>
</div>
</form>
</div>
</div>
<!-- /container -->
</body>
</html>
PHP
<?php
require 'database.php';
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( null==$id ) {
header("Location: index.php");
}
if ( !empty($_POST)) {
// keep track validation errors
$nameError = null;
$priceError = null;
$storeError = null;
$dateError = null;
$quantityError = null;
$portionError = null;
// keep track post values
$id = $_POST['id'];
$storeID= $_POST['storeID'];
$productName = $_POST['productName'];
$productPrice = $_POST['productPrice'];
$productQuantity = $_POST['productQuantity'];
$productPortion = $_POST['productPortion'];
$productComments = $_POST['productComments'];
$productDate = $_POST['productDate'];
//error displayed for creation errors
$valid = true;
if (empty($productName)) {
$nameError = 'Please enter the name of the product';
$valid = false;
}
if (empty($productPrice)) {
$priceError = 'Please enter a price';
$valid = false;
}
if (empty($storeID)) {
$storeError = 'Please enter a store';
$valid = false;
}
if (empty($productDate)) {
$dateError = 'Please enter the purchase date';
$valid = false;
}
if (empty($productComments)) {
$commentsError = 'Please enter any comments';
$valid = false;
}
if (empty($productQuantity)) {
$quantityError = 'Please select the quantity';
$valid = false;
}
if (empty($productPortion)) {
$portionError = 'Please enter the portion';
$valid = false;
}
// insert data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE Product SET productName=?, productPrice=?, storeID=?, productDate=?,
productComments=?, productQuantity=?, productPortion=? WHERE productID=?";
$q = $pdo->prepare($sql);
$q->execute(array($productName,$productPrice,$storeID,$productDate,
$productComments,$productQuantity,$productPortion,$id));
Database::disconnect();
header("Location: index.php");
}
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM Product WHERE productID = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$productName = $data['productName'];
$productPrice = $data['productPrice'];
$storeID = $data['storeID'];
$productQuantity = $data['productQuantity'];
$productPortion = $data['productPortion'];
$productComments = $data['productComments'];
$productDate = $data['productDate'];
Database::disconnect();
}
?>
Having a quick look at your code you are sending the form data via $_POST and on the php script checking $_GET then grabbing the id from $_REQUEST. Try changing
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
to
if ( !empty($_POST['id'])) {
$id = $_POST['id'];
}
Hope that helps!
Thanks Donniep!
I found that the answer was actually related to the POST values after being submitted. My impression was that I could still use the value from the GET call of 'id', but I instead needed to use the actual ID value from the product DB instead. The solution turned out to be:
// keep track post values
$id = $_POST['id'];
Needed to be changed to:
// keep track post values
$id = $_POST['productID'];
I am trying to update the records but the update query is not working for some reason.It is deleting and inserting fine but somehow the update doesn't work.I have checked various questions but couldn't find the answer.I have checked the data inserted in the query and its fine too.This is my code.
<?php
require 'database.php';
$ido = 0;
if ( !empty($_GET['id'])) {
$ido = $_REQUEST['id'];
echo $ido;
}
if ( !empty($_POST)) {
// keep track validation errors
$nameError = null;
$descError = null;
$priceError = null;
// keep track post values
$name = $_POST['name'];
$desc = $_POST['desc'];
$price = $_POST['price'];
// validate input
$valid = true;
if (empty($name)) {
$nameError = 'Please enter Name';
$valid = false;
}
if (empty($desc)) {
$descError = 'Please enter Valid descriptin';
$valid = false;
}
if (empty($price) || filter_var($price, FILTER_VALIDATE_INT) == false) {
$priceError = 'Please enter a valid price';
$valid = false;
}
// insert data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE Items SET I_name = ? , I_desc = ? ,I_price = ? WHERE I_id = ?"; <---This is the update query part
$q = $pdo->prepare($sql);
$q->execute(array($name,$desc,$price,$ido)); <---these are the values inserted
Database::disconnect();
header("Location: index.php");
}
}
else {
echo $ido;
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM Items where I_id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($ido));
$data = $q->fetch(PDO::FETCH_ASSOC);
$name = $data['I_name'];
$desc = $data['I_desc'];
$price = $data['I_price'];
Database::disconnect();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="span10 offset1">
<div class="row">
<h3>Update Items</h3>
</div>
<form class="form-horizontal" action="update_items.php" method="post">
<div class="control-group <?php echo !empty($nameError)?'error':'';?>">
<label class="control-label">Name</label>
<div class="controls">
<input name="name" type="text" placeholder="Item Name" value="<?php echo !empty($name)?$name:'';?>">
<?php if (!empty($nameError)): ?>
<span class="help-inline"><?php echo $nameError;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($descError)?'error':'';?>">
<label class="control-label">Description</label>
<div class="controls">
<input name="desc" type="text" placeholder="Item Description" value="<?php echo !empty($desc)?$desc:'';?>">
<?php if (!empty($descError)): ?>
<span class="help-inline"><?php echo $descError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($priceError)?'error':'';?>">
<label class="control-label">Price</label>
<div class="controls">
<input name="price" type="text" placeholder="Item Price" value="<? php echo !empty($price)?$price:'';?>">
<?php if (!empty($priceError)): ?>
<span class="help-inline"><?php echo $priceError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Create</button>
<a class="btn" href="index.php">Back</a>
</div>
</form>
</div>
</div> <!-- /container -->
</body>
</html>
This is your form:
<form class="form-horizontal" action="update_items.php" method="post">
^ nothing here
As you can see you are posting and there is no query variable after the url you are posting to.
Then you check for the ID:
$ido = 0;
if (!empty($_GET['id'])) {
$ido = $_REQUEST['id'];
echo $ido;
}
$ido will remain 0 as there is no $_GET['id'].
You can either modify your form to add the ID or add a hidden variable in the form with the ID and check for $_POST['id'].
I'd go for the second option:
<form class="form-horizontal" action="update_items.php" method="post">
<input type="hidden" name="id" value="<?php echo $ido; ?>">
and in php:
if (!empty($_POST)) {
$ido = $_POST['id'];
i have a problem with a form in codeigniter. i have tried different things but i can not find a solution.
this is the form:
<form method="post" action="<?php echo base_url(); ?>cms/cms/newUser">
<div class="row">
<div class="col-md-3">
Naam:
</div>
<div class="col-md-9">
<input type="text" id="naam" name="username" />
</div>
</div>
<div class="row">
<div class="col-md-3">
E-mail:
</div>
<div class="col-md-9">
<input type="email" name="mail" id="email" />
</div>
</div>
<div class="row">
<div class="col-md-3">
Passwoord:
</div>
<div class="col-md-9">
<input type="password" name="pass" id="password" />
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<input type="submit" name="aanmaken" class="btn btn-block btn-info" value="aanmaken" />
</div>
</div>
in my controller i process the data coming from the form.
the controller is located at controllers/cms/cms.php
class Cms extends MY_Controller
{
public function newUser()
{
if(isset($_POST))
{
echo "verwerk deze data <br>";
if(isset($_POST['username']))
{
echo 'naam is set!';
}
else {
echo 'naam is not set!';
}
$naam = $_POST['username'];
$mail = $_POST['email'];
$pass = $_POST['password'];
echo "naam: " . $naam . "<br>";
echo "mail: " . $mail . "<br>";
echo "pass: " . $pass . "<br>";
$time = new DateTime();
$time->setTimezone(new DateTimeZone ('Europe/Brussels'));
$tijd = $time->format('Y-m-d H:i:s');
include_once('application/libraries/PasswordHash.php');
$hasher = new PasswordHash(12,false);
$passwoord = $hasher->HashPassword($pass);
$gebruiker = new Gebruiker();
$gebruiker->naam = $naam;
$gebruiker->email = $mail;
$gebruiker->paswoord = $passwoord;
$gebruiker->laatsteLogin = $tijd;
$gebruiker->active = 0;
echo "coded pas" . $passwoord . "<br>";
}
else
{
echo "no post";
}
}
when i check my website and fill in the form i get 3 php error that the values that i put in the variable from the post does not exist. also the if(isset) function says that the variable is not set. i don't know why the form action doesn't work.
i even tried the codeigniter way but it gave the same result.
$name = $this->input->post('username');
thanks for the help.
HTML:
enter code here
<form method="post" action="<?php echo site_url(); ?>/cms/cms/newUser">
<div class="row">
<div class="col-md-3">
Naam:
</div>
<div class="col-md-9">
<input type="text" id="naam" name="username" />
</div>
E-mail:
<div class="row">
<div class="col-md-3">
Passwoord:
</div>
<div class="col-md-9">
<input type="password" name="pass" id="password" />
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<input type="submit" name="aanmaken" class="btn btn-block btn-info" value="aanmaken" />
</div>
</div>
PHP:
class Cms extends MY_Controller
{
public function newUser()
{
if(isset($_POST))
{
echo "verwerk deze data <br>";
if($this->input->post('username'))
{
echo 'naam is set!';
}
else {
echo 'naam is not set!';
}
$naam = $this->input->post('username');
$mail = $this->input->post('mail');
$pass = $this->input->post('pass');
echo "naam: " . $naam . "<br>";
echo "mail: " . $mail . "<br>";
echo "pass: " . $pass . "<br>";
$time = new DateTime();
$time->setTimezone(new DateTimeZone ('Europe/Brussels'));
$tijd = $time->format('Y-m-d H:i:s');
include_once('application/libraries/PasswordHash.php');
$hasher = new PasswordHash(12,false);
$passwoord = $hasher->HashPassword($pass);
$gebruiker = new Gebruiker();
$gebruiker->naam = $naam;
$gebruiker->email = $mail;
$gebruiker->paswoord = $passwoord;
$gebruiker->laatsteLogin = $tijd;
$gebruiker->active = 0;
echo "coded pas" . $passwoord . "<br>";
}
else
{
echo "no post";
}
}
$this->input->post('') will check automatically not need of isset().
$this->input->post('') will take name not id
In the form tag
<form method="post" action="<?php echo base_url(); ?>cms/cms/newUser">
cms is the name of the controller and you have used it twice. Try with
<form method="post" action="<?php echo base_url(); ?>cms/newUser">
you need to pass only controllername/func_name and keep your controller file in controller dir
then try in CI way
<?php echo form_open("cms/newUser");?>
<?php echo form_close();?>
or
<form method="post" action="<?php echo base_url(); ?>cms/newUser">
Else you need to use routing for this and tell to pick this controller on that route
for this go to config/routes.php and add route like:-
$route['cms'] = "cms/cms";
try to load form helper and use this code
controller:
$this->viewContent['form_action'] = base_url()."cms/cms/newUser";
$this->load->view('your_view_page',$this->viewContent);
view:
echo form_open($form_action);