php script was working, now second $_POST suddenly not working - php

I am fairly new to php and have been searching for three days to find my problem. The script below worked for two years, and now suddenly the second POST command never populates (it is the last lines of code, and when I echo $_POST['input']; nothing is ever there.) I have looked at var_dump$[_$POST] and it gets $formType, but never $input.
Why would this suddenly stop working? It goes to the next form, but nothing works because it all counts on $input being passed on.
I am running on a unix server, Network Solutions.
Any help is greatly appreciated!!
here is the code (sanitized names of directories and databases, obviously):
<?php
session_start();
if ($_SESSION['auth'] != "yes") {
header("Location: login.php");
exit();
}
if (isset($_REQUEST['Enter'])) {
header("Location: http://www.mysite.com/subdirectory/nextform.php");
} else if (isset($_REQUEST['Delete'])) {
header("Location: http://www.mysite.com/subdirectory/deleterow.php");
}
########################################
####checks what kind of service request######
#########################################
?>
<form name="form" method="post" action="<?php
echo htmlentities($_SERVER['PHP_SELF']);
?>">
<p><legend>Form Type</legend></p>
<p> <basefont size = "4"></p>
<p><label for="formType" required>*Select Type of Form:</label></p>
<p><select name="formType"></p>
<p><option value="">select</option></p>
<p><option value="Garda">Security Officer</option></p>
<p><option value="Surveil">Surveil</option></p>
<p><option value="EmployApp">Employment Application</option></p>
<p></select></p>
<p><input type="submit" name="Submit" value="Submit" /></p>
</form>
<?php
$formType = $_POST['formType'];
SESSION_register("formType");
if ($formType == Garda) {
// Connects to your Database
include("introGardaform.php");
$database = "Gardaform"; // provide your database name
$db_table = "GardaINQ"; // leave this as is
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database, $db);
$data = mysql_query("SELECT * FROM GardaINQ") or die(mysql_error());
// puts the "GardaINQ"database table info into the $info array
//$info = mysql_fetch_array( $data );
Print "<table border cellpadding=15>";
while ($info = mysql_fetch_array($data)) {
Print "<tr>";
Print "<th>Inquiry Number:</th> <td>" . $info['Inquiry_num'] . "</td> ";
Print "<th>Contact Name:</th> <td>" . $info['contactName'] . " </td>";
Print "<th>Contact Number:</th> <td>" . $info['contactNum'] . " </td></tr>";
}
Print "</table>";
}
if ($formType == Surveil) {
// Connects to your Database
include("investfm.php");
$database = "investigateform"; // provide your database name
$db_table = "surveil"; // leave this as is
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database, $db);
$data = mysql_query("SELECT * FROM surveil") or die(mysql_error());
// puts the "surveil"database table info into the $info array
//$info = mysql_fetch_array( $data );
Print "<table border cellpadding=15>";
while ($info = mysql_fetch_array($data)) {
Print "<tr>";
Print "<th>Inquiry Number:</th> <td>" . $info['Inquiry_num'] . "</td> ";
Print "<th>Contact Name:</th> <td>" . $info['contactName'] . " </td>";
Print "<th>Contact Number:</th> <td>" . $info['contactNum'] . " </td></tr>";
}
Print "</table>";
}
if ($formType == EmployApp) {
// Connects to your Database
include("introhires.php");
$database = "hires"; // provide your database name
$db_table = "hiresentry"; // leave this as is
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database, $db);
$data = mysql_query("SELECT * FROM hiresentry") or die(mysql_error());
// puts the "hiresentry"database table info into the $info array
//$info = mysql_fetch_array( $data );
Print "<table border cellpadding=15>";
while ($info = mysql_fetch_array($data)) {
Print "<tr>";
Print "<th>First Name:</th> <td>" . $info['firstName'] . " </td>";
Print "<th>Last Name:</th> <td>" . $info['lastName'] . " </td>";
Print "<th>Date:</th> <td>" . $info['date'] . " </td></tr>";
}
Print "</table>";
}
?>
<form name="finddata" method="POST" action="<?php
echo htmlentities($_SERVER['PHP_SELF']);
?>">
<p> <basefont size = "4"></p>
<p><label for="finddata" required>Enter Inquiry Number for Garda or Surveil, Last name for Employment
Application:</label></p>
<p><input type = "text" size="20" maxlength="40" required onKeyPress="return noenter()"
name="input"></p>
<p><input type="hidden" value="<?php
echo $formType;
?>" name="formType"></p>
<p><input type="submit" name="Enter" value="Enter" ></p><br/>
<p><input type="submit" style="font-face: 'Comic Sans MS'; font-size: larger; color: red; background-color: #FFFFC0; border: 3pt ridge lightgrey" name = "Delete" value="Delete"></p>
</form>
<?php
$input = $_POST['input'];
SESSION_register("input");
echo $_POST['input'];
?>

You have used SESSION_register("formType"); undefined function and This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
So you can use $_SESSION["formType"]=$formType;, also
Need to wrap " or ' to check the string. Try this,
if($formType=="Garda"){
and if($formType=="EmployApp"){
instead of
if($formType==Garda){
if($formType==EmployApp){

You have a lot of issues on your code.
You are comparing your $formtype variable to a constant instead. It has to be like this. if($formType=="Garda"){ and if($formType=="Surveil"){ and if($formType=="EmployApp"){ add the double quotes to all those if statements as shown.
You are using a deprecated version i.e. session_register

can you try the following:
Remove the action attribute completely from both the forms as by default it is POST to self.
replace the font-face css property to font-family property in the last element of the second form. however that should not be the cause of the issue you are facing.
As there are 2 forms in the page and there are 2 code blocks depending on the form post, I assume that your second form post creates the issue in the first part of the code as the data for the first from will not be posted from the submit button in the second form. so before executing the piece of code you should identify if that piece of code should be executed or not. A simple multi form setup should look something like this:
<form name="form1" method="post">
<input type="text"/>
<input type="submit" name="form1-submit" value="Submit Name" />
</form>
<!-- form1 specific code -->
<?php
if(isset($_POST["form1-submit"]))
{
// do your stuff here
}
?>
<form name="form2" method="post">
<input type="text"/>
<input type="submit" name="form2-submit" value="Submit Name" />
</form>
<!-- form2 specific code -->
<?php
if(isset($_POST["form2-submit"]))
{
// do your stuff here
}
?>
<!-- independent code -->
<?php
// do your common code here.
?>
If you can write the code in above manner you will be able to resolve your problem yourself. please let us know in case above helped.

Related

php: Unable to take value from the user by using submit button

This is my db : link link
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM org_insert");
echo "<!doctype html>
<html lang=\"en\">
<head>
<!-- Bootstrap CSS -->
<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css\">
<title>Hello, world!</title>
</head>
<body>
<table border='1'>
<tr>
<th>below_whom</th>
<th>name</th>
</tr>";
$row = mysqli_fetch_array($result);
#echo '<pre>'; print_r($row); echo '</pre>';
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['below_whom'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<div class="form-group">
<label for="usr">below_whom:</label>
<input type="text" name ="below_whom" id="below_whom" class="form-control">
</div>
<div class="form-group">
<label for="usr">name:</label>
<input type="text" name ="name" id="name" class="form-control">
</div>
<form method="post">
<input type="button" name="submit" id="submit" class="btn btn-primary" value="submit"/>
</form>
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit']))
{
if($below_whom !=''||$name !=''){
$below_whom=$_POST['below_whom'];
$name=$_POST['name'];
$query=mysqli_query("INSERT INTO org_insert VALUES ('$below_whom','$name');");
$query_run = mysqli_query($con,$query);
echo "<p>query inserted.</p>";
}else{
echo "<p>Insertion Failed.</p>";
}
}
mysqli_close($con);
echo"</body>
</html>";
?>
The text under p tag isn't getting executed, ie, the program is not going inside the if statement itself. I have rechecked the syntax, what is the problem? Is the syntax incorrect? I am pretty sure the connection with sql is correct. I have also refereed to some articles, still I am stuck here.
use post variables before if loop as shown below
if(isset($_POST['submit']))
{
$below_whom=$_POST['below_whom'];
$name=$_POST['name'];
if($below_whom !=''||$name !=''){
$query=mysqli_query("INSERT INTO org_insert VALUES ('$below_whom','$name');");
$query_run = mysqli_query($con,$query);
echo "<p>query inserted.</p>";
}else{
echo "<p>Insertion Failed.</p>";
}
}
and in HTML Code add type as submit and start form tag before div as
<form method="post" action=""> and closes after input tag
<input type="submit" name="submit" id="submit" class="btn btn-primary" value="submit"/>
One of the issues that I am able to see is that Your query should be:
$query=mysqli_query("INSERT INTO `org_insert`(`below_whom`,`name`) VALUES ('$below_whom','$name')");
Hope this helps.
Change the mysqli_fetch_array to mysqli_fetch_assoc or add a parameter too
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);

PHP SQL Convert Select Statement into Variable for DB Insert

I am currently in the process of creating a Quiz Builder, I am having a slight issue with some DB inserts. What I am trying to do within the code is insert a Quiz Title and Description to the Quiz table which will in turn create a Quiz ID. I want to insert this Quiz ID along with Class ID values from the checkbox. Any suggestions/advice would be greatly appreciated.
Note: I know I should be using prepare statements compared to what I am currently using. I am planning on fixing this issue once I get my main functionalities working.
<form method="post" action="#">
<p>
<label>Quiz Title: </label>
<input type="text" placeholder="Insert Quiz Title here" name="quizTitle" class="form-control" />
</p>
<p>
<label>Quiz Description: </label>
<input type="text" placeholder="Insert Quiz Description here" name="description" class="form-control" />
</p>
<?php
$showAllClasses = "SELECT * FROM class";
mysqli_query($mysqli, $showAllClasses) or die ('Error finding Classes');
$showClassesResult = mysqli_query($mysqli, $showAllClasses);
echo"<table border='1' cellpadding='10' align='center'>";
echo "<tr><th></th><th>Class ID</th><th>Class Name</th><th>Class
Description</th></tr>";
//while ($row = mysqli_fetch_assoc($result)){
while ($row = $showClassesResult->fetch_object()){
echo "<tr>";
echo "<td><input type='checkbox' id='" .$row->classID . "' name='check_box[]' value='" .$row->classID . "'></td>";
echo "<td>" .$row->classID . "</td>";
echo "<td>" .$row->className . "</td>";
echo "<td>" .$row->classDesc . "</td>";
//echo "<td><button type='button' name='add' id='add' data-toggle='modal' data-target='#questionType' class='btn btn-success'>Edit Students</button></td>";
echo "</tr>";
}
if (isset($_POST['submit'])) {
//Get POST variables
$quizTitle = '"' . $mysqli->real_escape_string($_POST['quizTitle']) . '"';
$description = '"' . $mysqli->real_escape_string($_POST['description']) . '"';
//echo $quizTitle;
//echo $description;
$getQuizIDQuery = "SELECT quizID FROM quiz ORDER BY quizID DESC LIMIT 1";
mysqli_query($mysqli, $getQuizIDQuery) or die ('Error getting Quiz ID');
$result = mysqli_query($mysqli, $getQuizIDQuery);
//$insertedQuizId = $mysqli->insert_id;
//Question query
$quizCreationQuery = "INSERT INTO quiz (quizTitle, description) VALUES($quizTitle, $description)";
foreach ($_POST['check_box'] as $classID) {
$ClassQuizQuery = "INSERT INTO quiz_class(classID, quizID) VALUES ('$classID', '$result')";
//$insert_ClassQuiz = $mysqli->query($ClassQuizQuery) or die($mysqli->error . __LINE__);
//Run Query
$insert_row = $mysqli->query($quizCreationQuery) or die($mysqli->error . __LINE__);
}
}
?>
</table>
<div align="center">
<input type="submit" name="submit" value="Submit"
class="btn btn-info"/>
</div>
</form>
Here's how a working example of your code could look like (with some of the suggested changes from the comments). I kept you original code mostly as is, but changed the placement and order within your script.
Serparating HTML and PHP is one of the things you should always aim for. Once you have all the PHP code closer together, remove duplicates or unnecessary stuff (including the superflous SELECT query for the last inserted id).
I added some comments for extra explanation. The rest ist mostly untouched =)
<?php
// first - all code that should always be executed,
// this will later be used in the html output.
$showAllClasses = "SELECT * FROM class";
$showClassesResult = $mysqli->query($showAllClasses) or die ('Error finding Classes');
// now the code that should only be executed on POST request
if (isset($_POST['submit'])) {
// we only want things to happen if all queries are successful,
// otherwise we end up with quizzes without class connections.
$mysqli->begin_transaction();
//Get POST variables
$quizTitle = '"' . $mysqli->real_escape_string($_POST['quizTitle']) . '"';
$description = '"' . $mysqli->real_escape_string($_POST['description']) . '"';
//Question query
$quizCreationQuery = "INSERT INTO quiz (quizTitle, description) VALUES($quizTitle, $description)";
$mysqli->query($quizCreationQuery) or die($mysqli->error . __LINE__);
// The $mysqli instance knows the last inserted id, so we can just use that.
$insertedQuizId = $mysqli->insert_id;
foreach ($_POST['check_box'] as $classID) {
$ClassQuizQuery = "INSERT INTO quiz_class(classID, quizID) VALUES ('$classID', $insertedQuizId)";
$mysqli->query($ClassQuizQuery) or die($mysqli->error . __LINE__);
}
// Everything should have worked so we can now commit the transaction
$mysqli->commit();
}
// now that we are done with everything, we start with the html output.
// since we have done all the complicated stuff above, all we have to care
// about, is the html output and iterating over our classes to create the html table.
?>
<form method="post" action="#">
<p>
<label>Quiz Title: </label>
<input type="text" placeholder="Insert Quiz Title here" name="quizTitle" class="form-control"/>
</p>
<p>
<label>Quiz Description: </label>
<input type="text" placeholder="Insert Quiz Description here" name="description" class="form-control"/>
</p>
<table border='1' cellpadding='10' align='center'>
<tr><th></th><th>Class ID</th><th>Class Name</th><th>Class Description</th></tr>
<?php while ($row = $showClassesResult->fetch_object()): ?>
<tr>
<td><input type='checkbox' id='<?= $row->classID ?>' name='check_box[]' value='<?= $row->classID ?>'></td>
<td><?= $row->classID ?></td>
<td><?= $row->className ?></td>
<td><?= $row->classDesc ?></td>
<td><button type='button' name='add' id='add' data-toggle='modal' data-target='#questionType' class='btn btn-success'>Edit Students</button></td>
</tr>
<?php endwhile ?>
</table>
<div align="center">
<input type="submit" name="submit" value="Submit" class="btn btn-info"/>
</div>
</form>

How to delete multiple rows from mysql database with checkbox using PHP?

I try to delete my data in "admin" database, but the delete button does not function.
This is my top part
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="admin"; // Database name
$tbl_name="admin"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
This is my checkbox code
<tbody>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['course_code']; ?></td>
<td><?php echo $rows['course_name']; ?></td>
<td><?php echo $rows['lecture_id']; ?></td>
<td><input name="checkbox[]" type="checkbox"
id="checkbox[]" value="<?php echo $rows['course_code'];?>"></td>
<td><form>
</form>
</td>
</tr>
<?php
}
?>
</tbody>
and, this is my button code
<input type='button' id="delete" value='Delete' name='delete'>
This is my php function code
<?php
if(isset($_POST['delete'])){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE course_code='$del_id'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysql_close();
?>
include all the input elements within your <form> tags: <form> all inputs are here </form>
update:
<input name = "checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['course_code'];?>">
to (id doesn't matter here):
<input name="checkbox[]" type="checkbox" value="<?php echo $rows['course_code'];?>"/>
and your button code:
<input type='button' id="delete" value='Delete' name='delete'>
to
<input type="submit" value="Delete"/>
set opening <form> tag to <form action="delete.php" method="post">
Note:
I assume below codes are in delete.php file. if not replace "delete.php" with that name in above opening form tag.
your delete.php file:
<?php
$cheks = implode("','", $_POST['checkbox']);
$sql = "delete from $tbl_name where course_code in ('$cheks')";
$result = mysql_query($sql) or die(mysql_error());
mysql_close();
?>
Note:
Since mysql_ will deprecate on future, better is use mysqli extension. But before use that, you have to enable it on your server. mysqli is a part of php and newer version of php has it but not enabled. To enable this, view php info page and find the path of php.ini file in "Loaded Configuration File" row on that page.
You can see php info page by loading below php file in the browser:
<?php
phpinfo();
?>
open that php.ini file in a text editor and un-comment or add a line extension=php_mysqli.dll at the extensions list there.
also search for "extension_dir" and open the directory it says and make sure php_mysqli.dll file is there.
(you may have .so extension if you not use windows OS)
Then restart your server and you are done!
By Fred -ii-
Using mysqli_ with prepared statements is indeed a better and
safer method. However, some will even suggest PDO, but even PDO
doesn't have some of the functionalities that mysqli_ offers;
strangely that. Even PDO needs sanitization. Many think that using PDO will solve injection issues, which is false.
-Thanks Fred.
try this code. it is working well.
connection.php
<?php $hostname_conection = "localhost"; /* this is the server name(assigned to variable) which is localhost since it runs on local machine */
$database_conection = "company"; /* this is the database name( assigned to variable)*/
$username_conection = "root"; /* user name (assigned to variable)*/
$password_conection = ""; /*password (assigned to variable) */
$conection = mysql_connect($hostname_conection, $username_conection, $password_conection) or trigger_error(mysql_error(),E_USER_ERROR); /* Mysql_connect function is used to conncet with database it takes three parameters server/hostname, username,and password*/
mysql_select_db($database_conection,$conection) or die(mysql_error("could not connect to database!")); /* Mysql_select is used to select the database it takes two parameters databasename and connection variable in this case $conection */
?>
multiple_delete.php
<?php require_once('conection.php'); ?>
<?php
in
/* now to display the data from the database which we inserted in above form we */ /* we make the query to select data from the table EMP */
$display = "select * from test_mysql";
$result = mysql_query($display, $conection) or die(mysql_error()); /* the query is executed and result of the query is stored in variable $result */
if ($result == FALSE) {
die(mysql_error()); /* displays error */
} ?> <h1 align="center"> Displaying Recods in Table </h1>
<form method="get" action="" id="deleteform" >
<table width="245" border="1" align="center">
<tr>
<td width="51">
<input type="submit" name="delete" id="button" value="delete" onclick="document.getElementById('deleteform').action = 'delete.php';document.getElementById('deleteform').submit();"/> <!--- here on clicking the button the form is submitted and action is set to delete.php Here we have used javaScript document refers to this whole page and now we can access any tag that has its id with help of getElementById() method and after the we specify the operation we want to perform in this case action and submit. --->
</td>
<td width="50">id</td>
<td width="55">name</td>
<td width="47">lastname</td>
</tr>
<?php
while ($rows = mysql_fetch_array($result))
{ /* here we make use of the while loop which fetch the data from the $result int array form and stores in $row now we can display each field from the table with $row[‘field_name’] as below */
?>
<tr>
<td>
<input type="checkbox" name="empids[]" value="<?php echo $rows['id']; ?>" /> <!--here with each checkbox we send the id of the record in the empids[] array --->
</td>
<td>
<?php echo $rows['id'] ?>
</td>
<td>
<?php echo $rows['lastname'] ?>
</td>
<td><?php echo $rows['name'] ?></td>
<?php } ?>
</tr>
</table>
</form> ?>
</body>
</html>
delete.php
<?php
require_once('conection.php');
?>
<?php
if (isset($_GET['delete'])) /* checks weather $_GET['delete'] is set*/
{
if (isset($_GET['empids'])) /* checks weather $_GET['empids'] is set */
{
$checkbox = $_GET['empids']; /* value is stored in $checbox variable */
if (is_array($checkbox))
{
foreach ($checkbox as $key => $your_slected_id) /* for each loop is used to get id and that id is used to delete the record below */
{
$q="DELETE FROM test_mysql WHERE id=$your_slected_id "; /* Sql query to delete the records whose id is equal to $your_slected_id */
mysql_query($q,$conection) ; /* runs the query */
}
header("location:multiple_delete.php"); /* Goes back to index.php */
}
} else
{
echo" you have not selected reords .. to delete";
}
} ?>
$sql = "SELECT * FROM blacklist";
$result = $link->query($sql);
$count=mysqli_num_rows($result);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc())
{
echo "<table>";
echo "<th>";
echo "<td>" . "ID: " . $row["id"]."</td>";
echo "<td>" . " Dial Target: " . $row["dial_target"]."</td>";
echo "<td>" . " Destination: " . $row["pozn"]."</td>";
echo "<td>" . " Date: " . $row["block_date"] . "</td>";
echo "<td>" . "<div class='background' style='position: relative; top:8px;'>" . "<form>" . "<input action='index.php' method='post' type='checkbox' name='chechbox[]' value='".$row["id"]."'/>" ."</form>" . "</div>" . "</td>";
echo "</th>";
echo "</table>";
echo "</br>";
}
}
else
{
echo "0 results";
}
if(isset($_POST['Delete']))
{
for($i=0;$i<$count;$i++)
{
$del_id = $checkbox[$i];
$del = "DELETE FROM blacklist WHERE Delete='$del_id'";
$result = $link->query($del);
}
if($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
<!-- DELETE BUTTON -->
<form>
<input type='Submit' id="Delete" value='Delete' name='Delete'/>
</form>
<?php
$args1 = array(
'role' => 'Vendor',
'orderby' => 'user_nicename',
'exclude' => $user_id.',1',
'order' => 'ASC'
);
$subscribers = get_users($args1); foreach ($subscribers as $user) {
$fvendorck = $wpdb->get_row("select * from wp_vandor where parent_id = '".$user_id."' and child_id = '".$user->id."'");
$isfavvendor = $fvendorck->child_id;
if(!empty($isfavvendor)) {
?>
<li><input type="checkbox" id="listID" value='<?php echo $user->id; ?>' name="chk1[]" checked=""/><?php echo $user->headline; ?></li>
<?php }else{ ?>
<li><input type="checkbox" id="listID" value='<?php echo $user->id; ?>' name="chk1[]" /><?php echo $user->headline; ?></li>
<?php } }?>
</ul>

Need to have edit button which changes field to editable; on submit it alters database

I made a nice flowchart for you since this is kind of complicated.
Basically, I want to have a list of fields from a column in the database, displayed in a table. They have an EDIT button ext to them, and when you click the edit button you can edit the field. Then you click submit and it alters the table with the new value.
I pretty much know how to do each of the individual things, but I am having trouble combining all these things into one.
Flowchart:
Code:
STEP 1:
<table>
<th>First Name</th>
<?php
// Collects data from "users" table
$data = mysql_query("SELECT * FROM users")
or die(mysql_error());
// array
$info = mysql_fetch_array( $data );
// print data
// Print out the contents of the entry
Print "<tr><td>";
Print "".$info['fname'] . " ";
Print "</td><td>";
Print "".$info['lname'] . " ";
Print "</td><td>";
Print "".$info['email'] . " <br>";
Print "</td></tr>";
// loop for each row in the column
while($info = mysql_fetch_array( $data ))
{
Print "".$info['fname'] . " ";
Print "</td><td>";
Print "".$info['lname'] . " ";
Print "</td><td>";
Print "".$info['email'] . " <br>";
Print "</td></tr>";
}
?>
</table>
STEPS 2 & 3: (incomplete or possibly inaccurate as well)
<?php
if(isset($_POST['submit'])){
$variable = "somethinghere";
$variable = "somethinghere";
$variable = "somethinghere";
$variable = "somethinghere";
$variable = "somethinghere";
$query = "UPDATE users
SET fname='$variable1'
WHERE $variable2='$variable3' AND $variable4='$variable5'
(fname, lname, email) VALUES ('".$_POST['fname']."', '".$_POST['lname']."', '".$_POST['email']."')";
mysql_query($query);
}else{
?>
<div class="content">
<form method="post">
<div><strong>First Name:</strong><span class="errortext">*</span></div>
<div><input name="fname" type="text" /></div>
<div><strong>Last Name:</strong><span class="errortext">*</span></div>
<div><input name="lname" type="text" /></div>
<div><strong>Email:</strong><span class="errortext">*</span></div>
<div><input name="email" type="text" /></div>
<div><input id="submit-button" value="submit" name="submit" type="submit" /></div>
</form>
<?php }?>
</div>
Using JQuery is the way to change it.
Here's a plugin: http://www.appelsiini.net/projects/jeditable/default.html ; http://www.appelsiini.net/projects/jeditable

Troubleshooting HTML and PHP / MySQL

Long time reader, first time poster. I am a novice PHP enthusiast, and I have a page that I have been working. Right now I have the DB connection working well and my SELECT statement is giving me the info needed. My problems are two fold (maybe more after this post; set your phasers to cringe):
At one point, I had the INSERT working, but it suddenly stopped and no amount of tweaking seems to bring it back. I have verified that the INSERT statement works in a seperate PHP file without variables.
When I did have the INSERT working, every refresh of the page would duplicate the last entry. I have tried tried several ways to clear out the $_POST array, but I think some of my experimenting lead back to problem #1.
<?php
$dbhost = "REDACTED";
$dbuser = "REDACTED";
$dbpass = "REDACTED";
$dbname = "guest_list";
// Create a database connection
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("DB's not here, man: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
// replacement for mysql_real_escape_string()
function html_escape($html_escape) {
$html_escape = htmlspecialchars($html_escape, ENT_QUOTES | ENT_HTML5, 'UTF-8');
return $html_escape;
}
// Posting new data into the DB
if (isset($_POST['submit'])) {
$first = html_escape($_POST['first']);
$last = html_escape($_POST['last']);
$contact = html_escape($_POST['contact']);
$associate = html_escape($_POST['associate']);
$insert = "INSERT INTO g_list (";
$insert .= "g_fname, g_lname, g_phone, g_association) ";
$insert .= "VALUES ('{$first}', '{$last}', '{$contact}', '{$associate}')";
$insert .= "LIMIT 1";
$i_result = mysqli_query($connection, $insert);
// I have verified that the above works by setting the varialble
// in the VALUES area to strings and seeing it update
}
$query = "SELECT * ";
$query .= "FROM g_list ";
$query .= "ORDER BY g_id DESC";
$q_result = mysqli_query($connection, $query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Guest List</title>
<link href="guest.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<h1>REDACTED</h1>
<h2>Guest Registry</h2>
</header>
<div class="container">
<div class="registry">
<form name="formup" id="main_form" method="post">
<fieldset>
<legend>Please enter your name into the registry</legend>
<p class="first">First Name:
<input type="text" name="first" value="" placeholder="One or more first names" size="64"></p>
<p class="last">Last Name:
<input type="text" name="last" value="" placeholder="Last name" size="64"></p>
<p class="contact">Phone Number or Email:
<input type="text" name="contact" value="" placeholder="" size="32"></p>
<p class="associate">Your relation?
<input type="text" name="associate" value="" placeholder="" size="128"></p>
<p class="submit">
<input type="submit" name="submit" title="add" value="submit" placeholder=""></p>
</fieldset>
</form>
</div>
</div>
<h3>Guest List:</h3>
<table>
<tr>
<th>Firstname(s)</th><th>Lastname</th>
<th>Phone or Email</th><th>Association</th>
</tr>
<?php while($guest = mysqli_fetch_assoc($q_result)) {
echo "<tr>" . "<td>" . $guest["g_fname"] . "</td>"
. "<td>" . $guest["g_lname"] . "</td>"
. "<td>" . $guest["g_phone"] . "</td>"
. "<td>" . $guest["g_association"] . "</td>" . "</tr>";
} ?>
</table>
<footer>
<div>Copyright <?php echo date("Y"); ?>, REDACTED, LLC.</div>
<?php
if (isset($connection)) {
mysqli_close($connection);
}
?>
</footer>
</body>
</html>
These two lines will fail:
$insert .= "VALUES ('{$first}', '{$last}', '{$contact}', '{$associate}')";
$insert .= "LIMIT 1";
Two problems here, all with the second line:
No SPACE between ) and LIMIT: )LIMIT 1 is your code;
LIMIT 1 in an INSERT is not allowed....

Categories