I have a crud system in PHP and creating an approval section. I've create an approval page for the products which works great if the box is checked. But if its not and the person puts in the form, the date is still inputed. How can I have it only input the date if the box is checked?
Here is the form...
// if the form was submitted
if($_POST){
$product->reviewed = $_POST['reviewed'];
$product->review_date = $_POST['review_date'];
// set product property values
// update the product
if($product->approve()){
echo "<div class='alert alert-success alert-dismissable'>";
echo "Product approvals were set.";
echo "</div>";
}
// if unable to update the product, tell the user
else{
echo "<div class='alert alert-danger alert-dismissable'>";
echo "Unable to approve product.";
echo "</div>";
}
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?id={$id}");?>" method="post">
<table class='table table-hover table-responsive table-bordered'>
<tr>
<td>Name</td>
<td><?php echo $product->name; ?></td>
</tr>
<tr>
<td>Review</td>
<td><?php
if (is_null($product->reviewed)){
?>
<div id="ck-button"><label><input type="checkbox" name="reviewed" value="<?php echo $userRow['user_name'];?>" id="checkbox"/><span>Approve</span>
<input type='hidden' name='review_date' class='form-control' value="<?php echo date("Y-m-d h:i:sa"); ?>"/>
</div> </label>
<?php ;
}elseif (isset($product->reviewed)){
echo "<div id='approved-button'>" . $product->reviewed . ", " . $product->review_date . "</div>";
}
?>
</td>
</tr>
<tr>
<td></td>
<td>
<button type="submit" class="btn btn-primary">Set Approvals</button>
</td>
</tr>
</table>
</form>
Here is the function in the Product class
//approval for products
function approve(){
$query = "UPDATE
" . $this->table_name . "
SET
reviewed = :reviewed,
review_date = :review_date
WHERE
id = :id";
$stmt = $this->conn->prepare($query);
$this->id=htmlspecialchars(strip_tags($this->id));
$this->reviewed=htmlspecialchars(strip_tags($this->reviewed));
$this->review_date=htmlspecialchars(strip_tags($this->review_date));
$stmt->bindParam(':id', $this->id);
$stmt->bindParam(":reviewed", $this->reviewed);
$stmt->bindParam(":review_date", $this->review_date);
// execute the query
if($stmt->execute()){
return true;
}
return false;
}
To check if a checkbox element has been 'checked' in PHP you do the following:
if(isset($_POST['myCheckbox'])) { ... }
Instead, you have
if($_POST) { ... }
Which is what your problem seems to be if I am understanding your question correctly.
Related
I'm trying to develop a small "To Do List" application. The data for the app is stored in a database, and it needs to perform all CRUD operations. As it is right now, Select, Insert, and Delete work just fine. I'm stuck on updating though. The index.php page is shown below:
<?php
session_start();
require_once 'connect.php';
if (isset($_POST['DeleteTask'])) {
$sqldelete = "DELETE FROM Tasks WHERE dbTaskID = :bvTaskID";
$stmtdelete = $db->prepare($sqldelete);
$stmtdelete->bindValue(':bvTaskID', $_POST['taskID']);
$stmtdelete->execute();
echo "<div>Task successfully deleted</div>";
}
if (isset($_POST['theSubmit'])){
echo '<p>New task added</p>';
$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);
if(empty($formfield['ffTaskName'])){$errormsg .= "<p>Task field is empty.</p>";}
if(empty($formfield['ffTaskDue'])){$errormsg .= "<p>Deadline field is empty.</p>";}
if ($errormsg != "") {
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
} else {
try {
$sqlinsert = 'INSERT INTO Tasks (dbTaskName, dbTaskDue, dbTaskDone)
VALUES (:bvTaskName, :bvTaskDue, :bvTaskDone)';
$stmtinsert = $db->prepare($sqlinsert);
$stmtinsert->bindValue(':bvTaskName', $formfield['ffTaskName']);
$stmtinsert->bindValue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtinsert->bindValue(':bvTaskDone', 0);
$stmtinsert->execute();
echo "<div><p>There are no errors. Thank you.</p></div>";
} catch(PDOException $e){
echo 'ERROR!!!' .$e->getMessage();
exit();
}
}
}
$sqlselect = "SELECT * from Tasks";
$result = $db->prepare($sqlselect);
$result->execute();
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>
<h1><u>To-Do List</u></h1>
<table border>
<tr>
<th>Task</th>
<th>Deadline</th>
<th>Status</th>
<th>Complete</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
while ($row = $result->fetch()) {
if ($row['dbTaskDone'] == 0) {
$status = "Unfinished";
} else {
$status = "Finished";
}
echo '<tr><td>' . $row['dbTaskName']
. '</td><td>' . $row['dbTaskDue']
. '</td><td>' . $status;
/*if ($status == "Unfinished"){
echo '</td><td>';
echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value"' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="CompleteTask" value="Complete Task">';
echo '</form>';
}*/
echo '</td><td>';
echo '<form action="updateTask.php" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';
echo '</form></td><td>';
echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="DeleteTask" value="Delete Task">';
echo '</td></tr>';
}
?>
</table>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="toDoForm">
<fieldset><legend>New Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $formfield['ffTaskName']; ?>"></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $formfield['ffTaskDue']; ?>"></td>
</tr>
</table>
<input type="submit" name = "theSubmit" value="Add Task">
</fieldset>
</form>
</body>
</html>
Each record displays an "Edit" button that grabs the PK from the "Tasks" table and sends it to the updateTask.php page:
<?php
require_once 'connect.php';
$errormsg = "";
if (isset($_POST['EditTask']) ) {
$formfield['ffTaskID'] = $_POST['taskID'];
$sqlselect = "SELECT * FROM Tasks WHERE dbTaskId = :bvTaskID";
$result = $db->prepare($sqlselect);
$result->bindValue(':bvTaskID', $formfield['ffTaskID']);
$result->execute();
$row = $result->fetch();
if( isset($_POST['theEdit']) )
{
$formfield['ffTaskID'] = $_POST['taskID'];
$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);
if(empty($formfield['ffTaskName'])){$errormsg .= "<p>Task field is empty.</p>";}
if(empty($formfield['ffTaskDue'])){$errormsg .= "<p>Deadline field is empty.</p>";}
if ($errormsg != "") {
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
} else {
try
{
$sqlUpdate = "UPDATE Tasks SET dbTaskName = :bvTaskName,
dbTaskDue = :bvTaskDue
WHERE dbTaskID = :bvTaskID";
$stmtUpdate = $db->prepare($sqlUpdate);
$stmtUpdate->bindvalue(':bvTaskName', $formfield['ffTaskName']);
$stmtUpdate->bindvalue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);
$stmtUpdate->execute();
}
catch(PDOException $e)
{
echo 'ERROR!!!' .$e->getMessage();
exit();
}
}
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="editForm">
<fieldset><legend>Edit Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $row['dbTaskName'];?>" ></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $row['dbTaskDue']; ?>"></td>
</tr>
<tr>
<th>Submit Changes</th>
<input type="hidden" name="taskID" value="<?php echo $_formfield['ffTaskID']; ?>">
<td><input type="submit" name="theEdit" value="Submit Changes">
</table>
</fieldset>
</form>
</body>
</html>
The Name and Deadline fields populate appropriately based on the PK value passed from the last page. However, whenever I press the "Submit Changes" button, the update doesn't seem to execute. The page just refreshes and I see the table data remains unchanged in the database.
Solved the problem!
There were several issues that I discovered.
1.) In updateTask.php, I had the second if-statement nested within the first one. So it was running the update query as the page loaded, with no change to the data. So the 'theEdit' button did nothing since since it required the previous if statement's condition to run.
2.) The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.
$_formfield
..should have been..
$formfield
At this point, the update query functions properly.
3.) The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this.
Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.
I need to Insert data to the DB using the form given below
<form action="OtherEventPayment.php" id="frmSignIn" method="post">
<input type="hidden" name="online_id" value="<?php echo $lid; ?>" >
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Item</th>
<th>No. of Participants</th>
<th>Tick the Items</th>
</tr>
</thead>
<tbody>
<tbody>
<?php
$sn ="1";
$id = $oth_event_id;
$stmt1 = $DB_con->prepare('SELECT * FROM oth_events_details
LEFT JOIN oth_event_category ON (oth_events_details.oth_evcat_id=oth_event_category.oth_evcat_id)
WHERE oth_event_id =:uid ORDER BY oth_event_det_id DESC');
$stmt1->execute(array(':uid'=>$id));
$stmt1->execute();
if($stmt1->rowCount() > 0)
{
while($row1=$stmt1->fetch(PDO::FETCH_ASSOC))
{
extract($row1);
?>
<tr>
<td><?php echo $sn; ?></td>
<td>
<?php echo $row1['oth_category'];?> -
<?php
$group =$row1['oth_catgroup_type'];
if ($group=="S")
{
echo "Single";
}
elseif ($group=="D")
{
echo "Doubles";
}
else{
echo "Group";
}
?>
</td>
<td><?php echo $row1['participntno']; ?></td>
<td>
<b>
</b>
<input type="checkbox" name="chk[<?php echo $row1['oth_event_det_id'];?>]" value="<?php echo $row1['oth_event_det_id'];?>" id="chk[<?php echo $row1['oth_event_det_id'];?>]" />
Fees:- <?php echo $row1['oth_ev_fee'];?>
</td>
</tr>
<?php $sn++; ?>
<?php
}
}
else
{
?>
<div class="col-xs-12">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> No Data Found ...
</div>
</div>
<?php
}
?>
</tbody>
</table>
<div class="col-md-6">
<input type="submit" name="selectItems" value="Submit & Proceed" class="btn btn-primary pull-right mb-xl" data-loading-text="Loading...">
</div>
</div>
<?php echo $sn1=$sn-1; ?>
</form>
in the OtherEventPayment.php i have written the code. But not working . How to Insert data correctly to DB
<?php
require_once 'dbconfig.php';
if(isset($_POST['selectItems']))
{
echo array[] = $_POST['chk[]'];
echo $oth_online_id= $_POST['online_id'];
if($oth_event_detid != ""){
for($i=0;$i<sizeof($oth_event_detid);$i++)
{
// oth_event_det_id,oth_online_id
$stmt = $DB_con->prepare('INSERT INTO othevents_itemsonline(oth_event_det_id,oth_online_id) VALUES( :oth_event_det_id, :oth_online_id)');
$stmt->bindParam(':oth_event_det_id',$oth_event_det_id);
$stmt->bindParam(':oth_online_id',$oth_online_id);
if($stmt->execute())
{
$lastonlineid= $DB_con->lastInsertId();
$successMSG = "Thank you For Registering with us . Please select the items to be participating...";
// header("refresh:0;OtherEventsOnlineRegistrationThankyou.php"); /
}
else
{
$errMSG = "error while registering....";
} } }
}
?>
Name should be same for input field. Use following code:
<input type="checkbox" name="chk[]" value="<?php echo $row1['oth_event_det_id'];?>" id="chk[<?php echo $row1['oth_event_det_id'];?>]" />
Fees:- <?php echo $row1['oth_ev_fee'];?>
You can see name. Hopefully it will be clear enough
Just change the value of you checkboxes and the value it represents but keep the name same with the others, yet it should have a name with []
<input type="checkbox" id="chk<?php echo $row1['oth_event_det_id'];?>" name="chk[]" value="<?php echo $row1['oth_event_det_id'];?>">
<label for="chk<?php echo $row1['oth_event_det_id'];?>"><?php echo $row1['oth_event_det_id'];?></label>
having a name chk[] like this will send and serve as an array in your get or post-function so loop it on controller or function that will add it in the DB
upon inserting it,
$data = $_GET['chk']; //this is in array form
foreach($data as $chk){
//insert code here
}
I am trying to update multiple rows simultaneously and I think my query is somewhat messed up while i am trying to update all my rows simultaneously. I am using CodeIgniter framework. I want to work on my query and the data being sent from the view but unable to get a logic to update all rows simultaneously. Please note that there should be only one update button as this is a requirement
My View code is:
<form action="" method="post">
<table border="1" style="background:none;width:100%;" RULES="ROWS" class="tab_data">
<thead>
<th width="30px">No</th>
<th >Action Item</th>
<th>Responsibility</th>
<th>Order</th>
<th>Mandatory?</th>
<th width = "100px" align="center">Actions</th>
</thead>
<tbody>
<?php
$serial_no = 1;
if(count($rows))
{
foreach($rows as $row)
{
?>
<tr>
<td><?php echo $serial_no; ?></td>
<td>
<?php
echo "<input type='hidden' class='col-md-4 form-control' name='checklist_id' value='"
.$row['checklist_id']."' />";
echo $row['action_item'];
?>
</td>
<td>
<?php echo $row['responsibility']; ?>
</td>
<td>
<input type="hidden" name="row_id[]" value="<?php echo $row['sequence']; ?>">
<input type="text" class="form-control" name="order[]" value="<?php echo $row['sequence']; ?>">
</td>
<td>
<input type="checkbox" class="" name="if_checklist<?php echo $row->checklist_id; ?>" value="1" echo 'checked'; >
</td>
<td align="center">
<?php
echo anchor('exits/delete_absconding_checklist/'.$row['checklist_id'],
"<i class='fa fa-trash-o' alt='Delete' title='Delete' rel='".$row['id']."' ></i>",
array('rel' => $row->id, 'class' => 'edit_row'));
?>
</td>
</tr>
<?php
$serial_no++;
}
}
?>
<tr>
<td></td><td></td><td></td><td></td>
<td>
<input type="hidden" name="action" value="update_order">
<button type="submit" name="submit" class="btn btn-info pull-right">Update</button>
</td>
</tr>
</tbody>
</table>
</form>
My Controller Code is:
function display_absconding_checklist()
{
if($this->input->post('action') == '_doDelete' || $this->input->post('action') == '_doChangeStatus')
{
$this->admin_init_elements->delete_rows('applicant_status');
}
if($this->input->post('action') == 'update_order')
{
$this->exit_common->update_absconding_checklist();
}
$data['rows'] = $this->exit_common->get_all_absconding_checklists();
$this->data['maincontent'] = $this->load->view('maincontents/backend_display_absconding_checklist', $data, true);
$this->load->view('layout', $this->data);
}
My Model code is:
function update_absconding_checklist($post_array)
{
$post_array = $this->input->post();
foreach($post_array['checklist_id'] as $key => $rowid)
{
if($rowid > 0)
{
if(isset($post_array['if_checklist'.$rowid]))
$in = '1';
else
$in = '0';
$sql1 = "UPDATE pr_absconding_checklists SET `action_item` = '"
.$post_array['action_item'][$key]
."', `sequence` = '".$post_array['sequence'][$key]
."', `if_checklist` = '".$in
."' WHERE `checklist_id` = ".$rowid;
}
else
{
$sql1 = "UPDATE pr_absconding_checklists SET `action_item` = '"
.$post_array['action_item'][$key]
."', `sequence` = '".$post_array['sequence'][$key]
."', `if_checklist` = '".$in."'";
}
$query1 = $this->db->query($sql1);
}
}
I am getting no errors but there are many errors in my code and i am messed up, i am attaching my table snapshot also, please recommend improvements
My Table name is pr_absconding_checklists
I am using editing file for updating data of mobile no, email details etc, but it is not updating , it shows the results of data so connection is working but no updating of data is there. code:
<?php
include('header.php');
$msg='';
?>
<div class="page-cont1">
<!--heading starts-->
<?php
session_start(); //starts the session
if($_SESSION['user']){ //checks if user is logged in
}
else{
header("location:index.php"); // redirects if user is not logged in
}
$user = $_SESSION['user']; //assigns user value
$id_exists = false;
?>
<body>
<h2>Home Page</h2>
<p>Hello <?php Print "$user"?>!</p> <!--Displays user's name-->
Click here to logout<br/><br/>
Return to Home page
<h2 align="center">Currently Selected</h2>
<table border="1px" width="100%">
<tr>
<th>Id</th>
<th>E-Mail</th>
<th>Mobile No</th>
<th>Details</th>
<th>Extra Information</th>
</tr>
<?php
if(!empty($_GET['id']))
{
$id = $_GET['id'];
$_SESSION['id'] = $id;
$id_exists = true;
$query = mysql_query("Select * from doctor Where id='$id'"); // SQL Query
$count = mysql_num_rows($query);
if($count > 0)
{
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print '<td align="center">'. $row['id'] . "</td>";
Print '<td align="center">'. $row['your_email'] . "</td>";
Print '<td align="center">'. $row['mobile_no'] . "</td>";
Print '<td align="center">'. $row['detail'] . "</td>";
Print '<td align="center">'. $row['info'] . "</td>";
Print "</tr>";
}
}
else
{
$id_exists = false;
}
}
?>
</table>
<br/>
<?php
if($id_exists)
{
Print '
<form action="edit.php" method="POST">
Enter new E-Mail: <input type="text" name="your_email"/><br/>
Enter new Mobile: <input type="text" name="mobile_no"/><br/>
Enter new detail: <input type="text" name="detail"/><br/>
Enter new Extra Information: <input type="text" name="info"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}
else
{
Print '<h2 align="center">There is no data to be edited.</h2>';
}
?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$field_email = mysql_real_escape_string($_POST['your_email']);
$field_phone = mysql_real_escape_string($_POST['mobile_no']);
$detail = mysql_real_escape_string($_POST['detail']);
$field_message = mysql_real_escape_string($_POST['info']);
mysql_query("UPDATE doctor SET your_email='$field_email', mobile_no='$field_phone', detail='$detail', info='$field_message' WHERE id='$id'") ;
header("location: home.php");
}
?>
<?php
include('footer.php');
$msg='';
?>
</body>
More over header file includes the connect file, and one query form is there problem lie in header file or problem in edit file.
First replace DOCTOR with doctor in update query (as Utharsh has stated).
Second:
You'll have to include the id in your form to be posted.
Print '<form action="edit.php" method="POST">
Enter new E-Mail: <input type="text" name="your_email"/><br/>
Enter new Mobile: <input type="text" name="mobile_no"/><br/>
Enter new detail: <input type="text" name="detail"/><br/>
Enter new Extra Information: <input type="text" name="info"/><br/>
<input type="hidden" name="id" value="'.$id.'">
<input type="submit" value="Update List"/>
</form>';
replace DOCTOR withdoctor in your update query.
If problem persists than try to hard code your query and execute it in phpmyadmin.
put a hidden field in your form with reference to the id as jeff stated
After receiving ans i studied my code in the ans i read about ID so i noticed that in updating there is no ID reference so i changed my code now problem solved my working code is:
<?php
include('header.php');
$msg='';
?>
<div class="page-cont1">
<!--heading starts-->
<?php
session_start(); //starts the session
if($_SESSION['user']){ //checks if user is logged in
}
else{
header("location:index.php"); // redirects if user is not logged in
}
$user = $_SESSION['user']; //assigns user value
$id_exists = false;
?>
<body>
<h2>Home Page</h2>
<p>Hello <?php Print "$user"?>!</p> <!--Displays user's name-->
Click here to logout<br/><br/>
Return to Home page
<h2 align="center">Currently Selected</h2>
<table border="1px" width="100%">
<tr>
<th>Id</th>
<th>E-Mail</th>
<th>Mobile No</th>
<th>Details</th>
<th>Extra Information</th>
</tr>
<?php
if(!empty($_GET['id']))
{
$id = $_GET['id'];
$_SESSION['id'] = $id;
$id_exists = true;
$query = mysql_query("Select * from doctor Where id='$id'"); // SQL Query
$count = mysql_num_rows($query);
if($count > 0)
{
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print '<td align="center">'. $row['id'] . "</td>";
Print '<td align="center">'. $row['your_email'] . "</td>";
Print '<td align="center">'. $row['mobile_no'] . "</td>";
Print '<td align="center">'. $row['detail'] . "</td>";
Print '<td align="center">'. $row['info'] . "</td>";
Print "</tr>";
}
}
else
{
$id_exists = false;
}
}
?>
</table>
<br/>
<?php
if($id_exists)
{
Print '
<form action="edit.php" method="POST">
Enter new E-Mail: <input type="text" name="your_email"/><br/><br/>
Enter new Mobile: <input type="text" name="mobile_no"/><br/><br/>
Enter new detail: <textarea name="detail" rows="6" id="detail" style="width:200px;"></textarea><br/><br/>
Enter new Extra Information: <textarea name="info" rows="4" id="info" style="width:200px;"></textarea><br/><br/>
<input type="hidden" name="id" value="'.$id.'">
<input type="submit" value="Update List"/>
</form>
';
}
else
{
Print '<h2 align="center">There is no data to be edited.</h2>';
}
?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$field_email = mysql_real_escape_string($_POST['your_email']);
$field_phone = mysql_real_escape_string($_POST['mobile_no']);
$detail = mysql_real_escape_string($_POST['detail']);
$field_message = mysql_real_escape_string($_POST['info']);
$id = $_SESSION['id'];
mysql_query("UPDATE doctor SET your_email='$field_email', mobile_no='$field_phone', detail='$detail', info='$field_message' WHERE id='$id'") ;
header("location: home.php");
}
?>
<?php
include('footer.php');
$msg='';
?>
</body>
I am trying to create a Registration System using only PHP. This is an example of that. But I think I have done something wrong. I tried to find similar solution in StackOverFlow posts but didn't get any exact solution. It would be really get if someone would help me to find the error in my code below.
<?php
// POST HANDLER -->
if(isset($_POST['registerForm']))
{
conFunc(); // Connection Function
$userid = $_POST['userid'];
$name = $_POST['name'];
$getUserId = mysql_query("SELECT * FROM `user` WHERE `userid` = '".$userid."'");
$id = mysql_fetch_array($getUserId);
if($id)
{
echo "This User ID is Already Available on the System, Please Choose Something Else!";
}
else
{
$query = mysql_query("INSERT INTO `user` (`userid`, `name`");
if($query)
{
echo "A New User is Registered Successfully:<br /><br />";
echo "<b>User ID:</b> " . $userid . "<br />";
echo "<b>User Name:</b> " . $name . "<br />";
}
else
{
echo "There is an Error while Saving: " . mysql_error();
echo "<br />Please click on Create User from menu, and try again<br /><br />.";
}
}
exit;
}
// POST HANDLER -->
?>
<!-- FORM GOES BELOW -->
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="registerForm">
<table style="width: 100%">
<tr>
<td>User ID</td>
<td><input name="userid" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td>Name</td>
<td><input name="name" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td></td>
<td><input style="width: 130px; height: 30px" type="submit" name="submit" value="Register Now" /><br /></td>
</tr>
</table>
</form>
You have to check the submit button is set or not.
if(isset($_POST['registerForm']))
should be
if(isset($_POST['submit'])) {
// your php code
} else {
// your html code
}
registerform element is not treated as post element so check with submit button.
Try following code :
<?php
// POST HANDLER -->
if(isset($_POST['submit'])){
conFunc(); // Connection Function
$userid = $_POST['userid'];
$name = $_POST['name'];
$getUserId = mysql_query("SELECT * FROM `user` WHERE `userid` = '".$userid."'");
$id = mysql_fetch_array($getUserId);
if($id)
{
echo "This User ID is Already Available on the System, Please Choose Something Else!";
}
else
{
$query = mysql_query("INSERT INTO `user` (`userid`, `name`");
if($query)
{
echo "A New User is Registered Successfully:<br /><br />";
echo "<b>User ID:</b> " . $userid . "<br />";
echo "<b>User Name:</b> " . $name . "<br />";
}
else
{
echo "There is an Error while Saving: " . mysql_error();
echo "<br />Please click on Create User from menu, and try again<br /><br />.";
}
}
exit;
}else{
// POST HANDLER -->
?>
<!-- FORM GOES BELOW -->
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="registerForm">
<table style="width: 100%">
<tr>
<td>User ID</td>
<td><input name="userid" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td>Name</td>
<td><input name="name" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td></td>
<td><input style="width: 130px; height: 30px" type="submit" name="submit" value="Register Now" /><br /></td>
</tr>
</table>
</form>
<?php } ?>
Your script should be like
$getUserId = mysql_query("SELECT id FROM `user` WHERE `userid` = '".$userid."'");
because you are getting all the results and you need to retrive the id only and your form action should be your same page itself
you need to read about mysql queries http://php.net/manual/en/book.mysql.php
also check your insert query not any data values inserted.
<?php if(isset($_POST['submit']))
{
conFunc(); // Connection Function
$userid = $_POST['userid'];
$name = $_POST['name'];
$getUserId = mysql_query("SELECT * FROM user WHERE userid = '".$userid."'");
$id = mysql_fetch_array($getUserId);
if($id)
{
echo "This User ID is Already Available on the System, Please Choose Something Else!";
}
else
{
$query = mysql_query("INSERT INTO user(userid, name) values('" .$userid . "','" . $name . "')";
if($query)
{
echo "A New User is Registered Successfully:<br /><br />";
echo "<b>User ID:</b> " . $userid . "<br />";
echo "<b>User Name:</b> " . $name . "<br />";
}
else
{
echo "There is an Error while Saving: " . mysql_error();
echo "<br />Please click on Create User from menu, and try again<br /><br />.";
}
}
exit;
}
// POST HANDLER -->
?>
<!-- FORM GOES BELOW -->
<form action="" method="post" name="registerForm">
<table style="width: 100%">
<tr>
<td>User ID</td>
<td><input name="userid" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td>Name</td>
<td><input name="name" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td></td>
<td><input style="width: 130px; height: 30px" type="submit" name="submit" value="Register Now" /><br /></td>
</tr>
</table>
</form>