I have struggled with this issue for a majority of the day and I am really stumped.
I have this page called preview.php with the following code below. Values are being passed to this page from another page called order.php. This preview.php page allows a user to review his or her data input from order.php.
If correction is needed, the user returns to order.php to make his or her corrections. If everything looks fine, the user sends his or her orders to process.php to be processed. The information sent to process.php are hidden form fields.
For some reason that I am unable to figure out, process.php is not recognizing the hidden form field values.
Does anyone have an idea how to resolve this?
This gentle man #spencer7593 actually helped me out with this insert statement. However, each time I attempt to add a record, I get the following:
Error: custname value cannot be null
This happens because the hidden form values are null and I can't figure out why.
preview.php
?php
error_reporting(E_ALL);
echo "DEBUG POST DATA: <pre>".print_r($_POST, 1)."</pre>";
if(isset($_POST['custname']))
$custname = $_POST['custname'];
if(isset($_POST['zip']))
$zip = $_POST['zip'];
if(isset($_POST['city']))
$city = $_POST['city'];
if(isset($_POST['email']))
$email = $_POST['email'];
$address = $_POST['address'];
$rowIDs = $_POST['rowIDs'];
$row2IDs = $_POST['row2IDs'];
echo $custname .'<br>';
echo $zip .'<br> <hr width=400 align=left>';
$rowIDs = $_POST['rowIDs'];
foreach ($rowIDs as $id) {
$agentname = $_POST['agentname' . $id];
$agentaddress = $_POST['agentaddress' . $id];
$agentincome = $_POST['agentincome' . $id];
echo 'Name: '. $agentname . '<br />';
echo 'Address: '. $agentaddress . '<br />';
echo 'agentincome: '. $agentincome . '<br /><br>';
}
?>
<body>
<form action='process.php' method = 'POST'>
<input type='hidden' name='custname' value="<?php echo $custname; ?>">
<input type='hidden' name='address' value="<?php echo $address; ?>">
<input type='hidden' name='email' value="<?php echo $email; ?>">
<input type='hidden' name='city' value="<?php echo $city; ?>">
<input type='hidden' name='zip' value="<?php echo $zip; ?>">
<input type='hidden' name='agentname' value="<?php echo $agentname; ?>">
<input type='hidden' name='agentaddress' value="<?php echo $agentaddress; ?>">
<input type='hidden' name='agentincome' value="<?php echo $agentincome; ?>">
<input type="action" type="button" value="Return to data" onclick="history.go(-1);" /> | <input type="submit" value="Submit your form" />
</form>
</body>
process.php
<?php
global $wpdb;
// Database connection
$conn = mysqli_connect("localhost","myuser","mypwd","myDB");
if(!$conn) {
die('Problem in database connection: ' . mysql_error());
}
// Data insertion into database
$sql = 'INSERT INTO `myDB`.`myTable` ( `custname`'
. ', `address`, `city`, `zip`, `email` )'
. ' VALUES ( ? , ? , ? , ? , ? )';
if( $sth = mysqli_prepare($conn,$sql) ) {
mysqli_stmt_bind_param($sth,'sssss'
,$_POST["custname"]
,$_POST["address"]
,$_POST["city"]
,$_POST["zip"]
,$_POST["email"]
);
if( mysqli_stmt_execute($sth) ) {
echo '<h1>Thank you</h1> <br> Go back to the main page <a href="index.php");';
} else {
printf("Error: %s\n",mysqli_stmt_error($sth));
}
} else {
printf("Error: %s\n",mysqli_connect_error($conn));
}
?>
Related
I am working on a project and would like to give the user per-determined values when updating a record.
Here is my code so far.
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br>
<strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br>
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$Name = mysql_real_escape_string(htmlspecialchars($_POST['Name']));
$Status = mysql_real_escape_string(htmlspecialchars($_POST['Status']));
$Comments = mysql_real_escape_string(htmlspecialchars($_POST['Comments']));
$Type = mysql_real_escape_string(htmlspecialchars($_POST['Type']));
// check that firstname/lastname fields are both filled in
if ($Name == '' || $Type == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($id, $Name, $Status, $Comments, $Type, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE Schools SET Name='$Name', Status='$Status', Comments='$Comments', Type='$Type' WHERE id='$id'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM Schools WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$Name = $row['Name'];
$Status = $row['Status'];
$Comments = $row['Comments'];
$Type = $row['Type'];
// show form
renderForm($id, $Name, $Status, $Comments, $Type, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>
I am wanting to replace the status text filed with a drop down list of options.
Replace your <input by <select :
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br>
<!-- <strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>-->
<strong>Status:</strong> <select name="Status">
<option value="1">Status 1</option>
<option value="2">Status 2</option>
</select>
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br>
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
If your statuses are in a table, fill the <select> with a query :
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br>
<!-- <strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>-->
<strong>Status:</strong> <select name="Status">
<?php
$result = mysql_query("SELECT * FROM tbl_status",$cnx);
while ( $row = mysql_fetch_array($result) )
echo "<option value='" . $row["id"] . "'>" . $row["text"] . "</option>";
?>
</select>
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br>
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
You could use the html <datalist> or the <select> tag.
I hope I could help.
First of all you need to switch from mysql_* to mysqli_* as it going to get removed in php 7.0 I'm using this function i created and it might help you
here is the php code
function GetOptions($request)
{
global $con;
$sql = "SELECT * FROM data GROUP BY $request ORDER BY $request";
$sql_result = mysqli_query($con, $sql) or die('request "Could not execute SQL query" ' . $sql);
while ($row = mysqli_fetch_assoc($sql_result)) {
echo "<option value='" . $row["$request"] . "'" . ($row["$request"] == $_REQUEST["$request"] ? " selected" : "") . ">" . $row["$request"] . "$x</option>";
}
}
and the html code goes like
<label>genre</label>
<select name="genre">
<option value="all">all</option>
<?php
GetOptions("genre");
?>
</select>
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I've got a database with a Users table which I'm trying to update.
Currently I have customers.php, which displays form fields with the user information so it can be updated.
This form points to edit_customer_processor.php , which takes the new values, puts them into a MYSQL query... and then despite the query working correctly when I query the DB via the PHPMyAdmin command line, the record doesn't update.
customers.php
<?php
session_start();
if(!$_SESSION["logged_in"]){
header("location:home.php");
die;
}
?>
<?php include 'header.html'; ?>
<div id='maincontent'>
<?php
if (isset($_GET["id"])){
$customer_id = $_GET["id"];
require_once('config.php');
$customer_query = "SELECT * FROM customer WHERE customer_id = $customer_id";
$customer_results = mysql_query($customer_query, $conn);
if (!$customer_results) {
die ("Error selecting car data: " .mysql_error());
}
else {
while ($row = mysql_fetch_array($customer_results)) {
echo "<h3>Edit Customer</h3>";
echo "<FORM method='post' action='edit_customer_processor.php'>";
echo '<p> Name: <input type="text" name="name" size = "40" value=' . $row[name] . '></p>';
echo '<p> Address: <input type="text" name ="address" size="40" value=' . $row[address] . '></p>';
echo '<p> Email: <input type="text" name="email" value=' . $row[email] . '></p>';
echo '<p> Phone: <input type ="text" name="phone" size="20" value=' . $row[phone] . '></p>';
echo '<input type ="hidden" name="customer_id" value="' . $row[customer_id] . '">';
echo '<input type ="hidden" name="formtype" value="edit_customer">';
echo '<input type="submit" name="submit" value= "Update">';
echo '</form>';
}
}
} else {
// If there isn't an ID, display the New Customer form and all customers below, with links
// to their edit pages.
echo "<h3>Enter new customer information and submit.</h3>";
echo "<FORM method='post' action='new_customer_processor.php'>";
echo '<p> Name: <input type="text" name="name" size = "40"></p>';
echo '<p> Address: <input type="text" name ="address" size="40"></p>';
echo '<p> Email: <input type="text" name="email"></p>';
echo '<p> Phone: <input type ="text" name="phone" size="20"></p>';
echo '<input type ="hidden" name="formtype" value="new_customer">';
echo '<input type="submit" name="submit" value= "Submit">';
echo '<input type ="reset" name="reset" value ="Reset">';
echo '</form>';
require_once('config.php');
echo "<h3>Current Customers</h3>";
$query = "SELECT * FROM customer";
$results = mysql_query($query, $conn);
if (!$results) {
die ("Error selecting customer data: " .mysql_error());
}
else {
// In the absence of an ID, all customers will be displayed down
// the bottom of the page
while ($row = mysql_fetch_array($results)) {
echo "<a href=customers.php?id=";
echo $row[customer_id];
echo "><p> $row[name] </p></a>";
echo "<p> $row[address] </p>";
echo "<p> $row[phone] </p>";
echo "<p> $row[email] </p>";
}
}
}
?>
Back to Customers Page
</div>
<?php include 'footer.html' ?>
edit_customer_processor.php
<?php include 'header.html' ?>
<div id="maincontent">
<?php
// Pulling in hidden customer ID from post value
$mysqli = new mysqli( 'localhost', 'root', 'root', 'w_c_a' );
// Check our connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert our data
$sql = mysql_query("UPDATE customer
SET name = '".mysql_real_escape_string($_POST['name'])."',
address = '".mysql_real_escape_string($_POST['address'])."',
phone = '".mysql_real_escape_string($_POST['phone'])."',
email = '".mysql_real_escape_string($_POST['email'])."'
WHERE customer_id='".mysql_real_escape_string($_POST['customer_id'])."'");
$update = $mysqli->query($sql);
echo "Customer updated: ";
echo "<a href=customers.php?id=" . $_POST['customer_id'] . ">";
echo "Back to Edit Customer</a>";
?>
</div>
<?php include 'footer.html' ?>
And when I echo the MYSQL query, I get:
UPDATE customer SET name = 'Kellyassdsa', address = 'ads', phone = '0260123123', email = 'asdasd' WHERE customer_id='1'
Which works when I put it in PHPMyAdmin.
I know it'll be some boneheaded little mistake, but I've been trying to get this work for ages now. Any ideas?
Maybe your program just can't connect to your MySQL database.
$customer_results = mysql_query($customer_query, $conn);
I can't see where you gave a value to the var $conn.
If the problem is connection problem then we might need your database info like the name of your table in PhpMyAdmin.
your problem is...
$sql = mysql_query(..);
$update = $mysqli->query($sql);
it should be
$sql = 'UPDATE ...';
$update = $mysqli->query($sql);
i think problem occurs due to line break. pleas make a query in single line without line break.
$sql = mysql_query("UPDATE customer SET name = '".mysql_real_escape_string($_POST['name'])."',address = '".mysql_real_escape_string($_POST['address'])."', phone = '".mysql_real_escape_string($_POST['phone'])."', email = '".mysql_real_escape_string($_POST['email'])."' WHERE customer_id='".mysql_real_escape_string($_POST['customer_id'])."'");
Hope this helps..
I try to send couple values from html form into database but variables are empty.
If I print variables in php area like echo $ad1_uid; I get the value.
After sending - all values are empty:
author_uid=&state=complete&mail=&user_uid=
Where could be a reason?
<form action="" method="get">
<input type="hidden" value="<?php ''.$ad1_uid; ?>" name="author_uid">
<input type="hidden" value="complete" name="state">
<input type="hidden" value="<?php ''.$user->mail; ?>" name="mail">
<button name="user_uid" type="submit" value="<?php ''.$user->uid; ?>">Zapisuje się</button>
</form>
<?php
}
$wyslany_user_uid = $_GET['user_uid'];
$wyslany_author_uid = $_GET['author_uid'];
$wyslany_mail = $_GET['mail'];
$wyslany_state = $_GET['state'];
var_dump($wyslany_mail);
mysql_query("INSERT INTO `krajeto_demo`.`registration1`
(user_uid, author_uid, mail, state ) VALUES (
'" . $wyslany_user_uid . "', '" . $wyslany_author_uid. "', '" . $wyslany_mail . "','" . $wyslany_state . "'
");
?>
To output the value of $ad1_uid in the form, try this instead:
<input type="hidden" value="<?= $ad1_uid; ?>" name="author_uid">
Or this if your setting don't
<input type="hidden" value="<?php echo $ad1_uid; ?>" name="author_uid">
Your PHP is not set to echo data into the form.
I've fixed those and added a debug clause to run. Just set to false once you've ran it to output data.
<form action="" method="get">
<input type="hidden" value="<?php echo $ad1_uid; ?>" name="author_uid">
<input type="hidden" value="complete" name="state">
<input type="hidden" value="<?php echo $user->mail; ?>" name="mail">
<button name="user_uid" type="submit" value="<?php echo $user->uid; ?>">Zapisuje się</button>
</form>
<?php
}
$debug = true;
if ($debug === true)
{
echo '<hr />
<h4>Debug Area</h4>
<pre>';
print_r(array($_GET, $_POST));
echo '</pre>
<hr />';
}
$wyslany_user_uid = $_GET['user_uid'];
$wyslany_author_uid = $_GET['author_uid'];
$wyslany_mail = $_GET['mail'];
$wyslany_state = $_GET['state'];
mysql_query("INSERT INTO `krajeto_demo`.`registration1` (user_uid, author_uid, mail, state ) VALUES (
'" . $wyslany_user_uid . "', '" . $wyslany_author_uid. "', '" . $wyslany_mail . "','" . $wyslany_state . "'
");
Try that and let me know what is echo'd to you.
I'm trying to integrate CKEditor into my simple CMS. I got it to show up on the page, but it's just above everything. I'm wondering how to get it into the correct spot, below my title textbox? Here is my code:
require_once 'conn.php';
include_once 'ckeditor/ckeditor.php';
$CKEditor = new CKEditor();
$CKEditor->editor('body');
$title= '';
$body= '';
$article= '';
$author_id= '';
if (isset($_GET['a'])
and $_GET['a'] == 'edit'
and isset($_GET['article'])
and $_GET['article']) {
$sql = "SELECT title, body, author_id FROM cms_articles " .
"WHERE article_id=" . $_GET['article'];
$result = mysql_query($sql, $conn) or
die ('Could not retrieve article data: ' . mysql_error());
$row = mysql_fetch_array($result);
$title = $row['title'];
$body = $row['body'];
$article = $_GET['article'];
$author_id = $row['author_id'];
}
require_once 'header.php';
?>
<form method="post" action="transact-article.php">
<h2>Compose Article</h2>
<p>
Title: <br />
<input type="text" class="title" name="title" maxlength="255" value="<?php echo htmlspecialchars($title); ?>" />
</p>
<p>
Body: <br />
<textarea class="body" name="body" id="body" rows="10" cols="60"><?php echo htmlspecialchars($body); ?></textarea>
</p>
<p>
<?php
echo '<input type="hidden" name="article" value="' .
$article . "\" />\n";
if ($_SESSION['access_lvl'] < 2) {
echo '<input type="hidden" name="author_id" value="' .
$author_id . "\" />\n";
}
if ($article) {
echo '<input type="submit" class="submit" name="action" ' .
"value=\"Save Changes\" />";
} else {
echo '<input type="submit" class="submit" name="action" ' .
"value=\"Submit New Article\" />";
}
?>
</p>
</form>
Personally I don't think you need the PHP library. Just add
<div contenteditable="true">
Editable text
</div>
as your editable and then just the script to get it running:
<script type="text/javascript" src="/path/to/ckeditor/ckeditor.js"></script>
That said, you may be able to pass the id of your textarea to the PHP library. To avoid confusion with the body tag, rename the id and name of this control to editable_content or similar. And as I mention above, try using a div instead.
I need to insert some form values into a db table , how would I create a function to call once the user clicks on the button and run the insert scrtipt .
<form name="quiz_info" method="post">
<?php
echo $this->quiz->title;
echo $mainframe->getPageTitle();
echo '<p><input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed">' . JText::_('I have Read and Acknowledge the procedure'). '</label></p>' ;
echo '<input id="proceedButton" name="proceedButton" disabled="true" value="' . JText::_('Acknowledge') . '" type="submit" />' ;
//Declare Variables
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$courseTitle = $mainframe->getPageTitle();
$db = &JFactory::getDBO();
$query ="INSERT INTO `jos_jquarks_users_acknowledge` (course_name,user_id,employeeNumber,department,name,acknoledge,timeStamp) VALUES ($courseTitle,$id,$username,$department,$name,acknoledge,vardate)";
$db->setQuery( $query );
$db->query();
?>
<input type="hidden" name="layout" value="default" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
PHP is executed on the server-side. If you want to call the function without reloading the page you will have to use an AJAX call, for example with jQuery.
You can find billions of tutorials through google.
<?php
if ($_POST['proceedButton'] != '') {
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$courseTitle = $mainframe->getPageTitle();
$db = &JFactory::getDBO();
$query ="INSERT INTO `jos_jquarks_users_acknowledge`(course_name,user_id,employeeNumber,department,name,acknoledge,timeStamp) VALUES ($courseTitle,$id,$username,$department,$name,acknoledge,vardate)";
$db->setQuery( $query );
$db->query();
}
?>
<form name="quiz_info" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo $this->quiz->title; ?>
<?php echo $mainframe->getPageTitle(); ?>
<input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed"><?php echo JText::_('I have Read and Acknowledge the procedure'); ?></label>
<input id="proceedButton" name="proceedButton" disabled="true" value="<?php JText::_('Acknowledge'); ?>" type="submit" />
<input type="hidden" name="layout" value="default" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
PHP doesn't work this way. If you wish to activate your PHP script on form submit, you'll need to submit the form to a PHP page, and on that page to put your script, for example
<form action=submit.php method=post>
<input type=text name=text>
<input type=submit>
</form>
Next, on submit.php:
<?php
if (!empty($_POST['text'])) { //If the POST variable set by input named 'text' is not empty...
echo $_POST['text']; //Print it on the screen.
} else { //If it is empty
echo "No form submission detected"; //Print an error
}
?>
If you want it to work without a page reload, you'll have to use some client side technology. The most popular one for that purpose is AJAX