Why do I keep getting Notice: Array to string conversion? - php

I am trying to update a mysql database but every time I get the Notice: Array to string conversion. I really can't figure out what I'm doing wrong here. Can someone kindly help?
My form
<div class="panel-body">
<form role="form" method="post" action="client_post.php" class="form-horizontal">
<fieldset>
<div class="form-group">
<label class="col-sm-3 control-label">Hospital No:
</label>
<div class="col-sm-5">
<input required class="form-control" name="Hospital_no" placeholder="Patients' Hospital Number">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Date of New Status</label>
<div class="form-group">
<label class="col-sm-3 control-label">New Status</label>
<div class="col-sm-5">
<select required name ="art_status" class="form-control">
<option></option>
<option value = "ART Restart" name="ART Restart">ART Restart</option>
<option value = "ART Transfer Out" name="ART Transfer Out" >ART Transfer Out</option>
<option value = "Pre ART Transfer Out" name="Pre ART Transfer Out">Pre ART Transfer Out</option>
<option value = "Lost to Followup" name="Lost to Followup">Lost to Followup</option>
<option value = "Stopped Treatment" name="Stopped Treatment">Stopped Treatment</option>
<option value = "Known Death" name="Known Death">Known Death</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-5">
<input class="btn btn-primary" type="submit" value="Update" name="submit" >
</div>
</div>
</fieldset>
</form>
I'm trying to make the update from this page (client.php)
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
$Hosp_no = ['Hospital_no'];
$art_status = ['art_status'];
$art_date = ['art_start_date'];
$update_client = "UPDATE `pat_reg` SET `art_status` = '$art_status', `art_start_date` = '$art_date' WHERE `Hospital_no` = '$Hosp_no'";
if (mysqli_query($dbcon,$update_client))
{
echo"<script>alert('Record Updated')</script>";
//echo"<script>window.open('client_update.php','_self')</script>";
}
$dbcon->close();
}
Please help.

Using the $_POST superglobal allows you to access the values submitted in the form.
Change the following:
$Hosp_no = ['Hospital_no'];
$art_status = ['art_status'];
$art_date = ['art_start_date'];
to this:
$Hosp_no = $_POST['Hospital_no'];
$art_status = $_POST['art_status'];
$art_date = $_POST['art_start_date'];
PS. You should be escaping the user's input before querying the database (or, ideally, use prepared statements) or you may find yourself being hacked very quickly if released to the public.

Related

Why php form not submitting?

I'm developing a script for online admission in a website. Below is php code of the page. The problem is that it's not submitting.
<?php
include ("include/header.php"), include ("include/config.php");
if(isset($_POST['applyAdmission'])) {
$admission_no = $_POST['admission_no'];
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$p_add = $_POST['p_add'];
$c_add = $_POST['c_add'];
$dob = $_POST['dob'];
$education = $_POST['education'];
$mobile = $_POST['mobile_no'];
$course = $_POST['course'];
$subjects = $_POST['subjects'];
$timing = $_POST['timing'];
$filepath_pic = $_FILES['picture']['name'];
$res_move_pic = move_uploaded_file($_FILES['picture']['tmp_name'], "/admission/".$filepath_pic);
$filepath_sign = $_FILES['sign']['name'];
$res_move_sign = move_uploaded_file($_FILES['sign']['tmp_name'], "/admission/".$filepath_sign);
$agree_terms = $_POST['agree_terms'];
$agree_cond = $_POST['agree_cond'];
if ($res_move_pic == 1 && $res_move_sign == 1 ) {
$query = "INSERT into online_admission (f_name, l_name, p_add, c_add, dob, degree, mobile_no, course, subjects, timing, pic, sign, agree_terms, agree_cond, applied_on)
values ('$f_name','$l_name','$p_add','$c_add','$dob','$education','$mobile','$course','$subjects','$timing','$filepath_pic','$filepath_sign','$agree_terms','$agree_cond','now()')";
$res = mysql_query($query) or die("ERROR: Unable to insert into database.");
if ($res == 1) {
header('Location:http://adarshclasses.in/admission_success.php/');
exit();
} else {
header('Location:http://adarshclasses.in/admission_failed.php/');
exit();
}
} else {
echo "Error in updateing profile pic and sign";
}
} else {
//echo "Please submit the form, thanks!";
}
;?>
Everything in form is correct like I added same name in form which i used in $_POST but still it's not working, please help me to fix this issue.
Here is html codes of form:
<form class="form-horizontal" id="admission_form" method="post" action="" enctype="multipart/form-data">
<!--div class="row">
<div class="col-lg-6">
<label for="admission_no"> Admission No. </label>
<input type="hidden" class="form-control" name="admission_no" value="<?php echo $admission_no ;?>" readonly disabled>
</div>
</div--><br>
<div class="row">
<div class="col-lg-6">
<label for="f_name"> First Name <span class="required">*</span> </label>
<input type="text" class="form-control" name="f_name" placeholder="Your first name" value="<?php echo $f_name ;?>" required>
</div>
<div class="col-lg-6">
<label for="l_name"> Last Name <span class="required">*</span></label>
<input type="text" class="form-control" name="l_name" placeholder="Your last name" value="<?php echo $l_name ;?>" required>
</div>
</div><br>
<div class="row">
<div class="col-lg-12">
<label for="p_add"> Permanent Address <span class="required">*</span></label>
<textarea class="form-control" name="p_add" placeholder="Please write your permanent address" value="<?php echo $p_add ;?>" required></textarea>
</div>
</div><br>
<div class="row">
<div class="col-lg-12">
<label for="c_add"> Current Address in Jodhpur <span class="required">*</span></label>
<textarea class="form-control" name="c_add" placeholder="Please write your address where you currently living" value="<?php echo $c_add ;?>" required></textarea>
</div>
</div><br>
<div class="row">
<div class="col-lg-6">
<label for="dob"> Date of birth <span class="required">*</span></label>
<input type="date" class="form-control" name="dob" placeholder="Your date of birth eg:- 25/11/1996" value="<?php echo $dob ;?>" required>
</div>
<div class="col-lg-6">
<label for="education"> Recent passed degree/exam - </label>
<input type="text" class="form-control" name="education" placeholder="for example - BA/ B.Sc etc." value="<?php echo $education ;?>" >
</div>
</div><br>
<div class="row">
<div class="col-lg-6">
<label for="mobile_no"> Mobile Number <span class="required">*</span></label>
<input type="number" class="form-control" name="mobile_no" placeholder="Enter your mobile number, eg - 8384991980" value="<?php echo $mobile_no ;?>" required>
</div>
<div class="col-lg-6">
<label for="course"> Select course <span class="required">*</span> </label>
<select class="form-control" name="course" required>
<option value="none"> --- Select one course --- </option>
<option value="IAS"> IAS </option>
<option value="RAS"> RAS </option>
<option value="Police constable"> Police constable </option>
<option value="SI"> SI </option>
<option value="Railway"> Railway </option>
<option value="REET"> REET </option>
<option value="Teacher"> Teacher </option>
<option value="Patwar"> Patwar </option>
<option value="Bank PO"> Bank PO </option>
<option value="Jr Accountant"> Jr Accountant </option>
<option value="Rajasthan police"> Rajasthan police </option>
<option value="SSC (10+2)"> SSC (10+2) </option>
</select>
</div>
</div><br>
<div class="row">
<div class="col-lg-6">
<label for="subjects"> Subjects - </label>
<input type="text" class="form-control" name="subjects" placeholder="Enter your subject you want to read" value="<?php echo $subjects ;?>" required>
</div>
<div class="col-lg-6">
<label for="timing"> Classes Timing - </label>
<input type="text" class="form-control" name="timing" placeholder="Your preferred time for coaching" value="<?php echo $timing ;?>" required>
</div>
</div><br>
<div class="row">
<div class="col-lg-6">
<label for="picture"> Upload your picture <span class="required">*</span></label>
<input type="file" class="form-control" name="picture" required>
</div>
<div class="col-lg-6">
<label for="sign"> Upload your signature <span class="required">*</span></label>
<input type="file" class="form-control" name="sign" required>
</div>
</div><br>
<div class="row">
<div class="col-md-12">
<input type="checkbox" aria-label="..." name="agree_terms" value="1"> I agree with Rules and Regulations mentioned below.<br>
<input type="checkbox" aria-label="..." name="agree_cond" value="1"> I hearbly declare that Adarsh Classes can use my pictures after my selection for advertising purpose.
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<button type="text" name="submit" class="btn btn-success btn-lg btn-block" name="applyAdmission"> Submit my application form </button>
</div>
</div>
</div>
</form>
The reason behind that in the input type of the HTML Page for the submit you are using <input type="button"
instead of <input type="submit". Use <input type="submit" that's work.
Example:
<input type="submit" name="" value="Submit">
Changed
<button type="text">
to
<button type="submit">
Change
button type="text" to type="button" Or input type ="submit/button"
You need to change this code:
<button type="text" name="submit" class="btn btn-success btn-lg btn-block" name="applyAdmission"> Submit my application form </button>
with below code :
<input type="submit" name="applyAdmission" value="Submit my application form" class="btn btn-success btn-lg btn-block" />
You also need to make sure that your wrote PHP code in same file, otherwise you have to add PHP file name in action tag in below line:
<form class="form-horizontal" id="admission_form" method="post" action="" enctype="multipart/form-data">
You also have some PHP error in your code, so you have to add first line in your PHP code and then fix your PHP Fatal error.
ini_set('display_errors', '1');
I see a little syntax error and I think fixing this will fix your issue.
Change
include ("include/header.php"), include ("include/config.php");
to
include ("include/header.php");
include ("include/config.php");
To show you the syntax error, here is an example:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
include("test.php"), include("someother.php");
The response:
Parse error: syntax error, unexpected ',' in ...\tests\includeTest.php on line 6
Incorrect input type
You should also change your button type.
Change
<button type="text"...
to
<button type="submit"...
Change <button> to <input>. Buttons can work with javascript but with only php button cant work with post data. You can not get POST data by <button>. For this you have to use <input>
Change this
<button type="text" name="submit" class="btn btn-success btn-lg btn-block" name="applyAdmission"> Submit my application form </button>
to
<input type="submit" name="applyAdmission">
Second:
Here is looking syntax error include ("include/header.php"), include ("include/config.php");
PHP requires instructions to be terminated with a semicolon at the end of each statement. Make them seperate by ; not by ,.
include ("include/header.php");
include ("include/config.php");
You can see documentation for more deep information

HTML <select> not submitted

I'm trying to submit a form to my PHP validation script and I get 2 undefined indexes. Both of which are an select tag (school and function, focus on the function for this question). I've looked through several posts, and I still don't see what's wrong.
Here's the form:
<form action="actions/user_edit.php?id='. $user['id'] .'" method="post" id="form_editUser" class="col s12">
<div class="row">
<div class="input-field col s12">
<input value="'.$user['name'].'" id="name" name="name" type="text" class="validate">
<label for="name">Naam</label>
</div>
<div class="input-field col s12">
<input value="'.$user['email'].'" id="email" name="email" type="text" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field col s12">
<select id="school" name="school">
<option>Ignore this select kthxbai
<option>
</select>
<label>School</label>
</div>
<div class="input-field col s12">
<select id="function" name="function">
<option value="" disabled>Kies functie</option>
<option value="Admin" >Admin</option>
<option value="Directeur" >Directeur</option>
<option value="Docent" >Docent</option>
</select>
<label>Functie</label>
</div>
</div>
</form>
<div class="modal-footer">
<input type="submit" value="Bewerk" form="form_editUser" class="modal-action modal-close waves-effect waves-green btn">
Annuleer
</div>
Here's the PHP script:
require '../config/db.php';
$userId = $_GET['id'];
$name = mysqli_real_escape_string($conn, $_POST["name"]);
$email = mysqli_real_escape_string($conn, $_POST["email"]);
$schoolId = mysqli_real_escape_string($conn, $_POST["school"]);
$function = mysqli_real_escape_string($conn, $_POST["function"]);
Turns out my form was inside a tbody element, which is not allowed. Problem solved, thanks for all the help!
Make sure to check your $_POST[] variables for existence, eg.
$option = isset($_POST['school']) ? $_POST['school'] : false;
if ($option) {
$schoolId = mysqli_real_escape_string($conn, $_POST["school"]);
}
I have seen cases where if nothing was selected as an option, the same is not included in the POST payload itself.

An empty row getting inserted in database

Hey I am trying to get this code running for the past few days now. I do not know what is the problem. Whenever I run the code I can see it running but an empty row gets inserted. Basically I ave tried to hard code the data and the data gets inserted. Here is the HTML form:
<form action="register.php" id="contactForm" type="post">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>First name *</label>
<input type="text" class="form-control" name="fname" >
</div>
<div class="col-md-6">
<label>Last name *</label>
<input type="text" class="form-control" name="lname" >
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Gender *</label><br>
<select name="gender">
<option> Male </option>
<option> Female </option>
</select>
</div>
<div class="col-md-6">
<label>Stream *</label><br>
<select name="stream">
<option> B-Tech </option>
<option> M-Tech </option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Email *</label>
<input type="text" class="form-control" name="email" >
</div>
<div class="col-md-6">
<label>Mobile *</label>
<input type="text" class="form-control" name="mobile">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>College *</label>
<input type="text" class="form-control" name="college" >
</div>
<div class="col-md-6">
<label>Job Kind *</label>
<input type="text" class="form-control" name="job" >
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
&nbsp&nbsp&nbsp&nbsp
<input type="submit" value="Register" class="btn btn-primary btn-lg"
data-loading-text="Loading..." name="submit">
</div>
</div>
</form>
Here is the registration.php
<?php
$connection = mysql_connect("EDITED by billy, was an I.P and port number", "user", "password"); // Establishing Connection with Server
$db = mysql_select_db("Registrations_connect", $connection); // Selecting Database from Server
$first_name = $_POST["fname"];
$last_name = $_POST["lname"];
$sex = $_POST["gender"];
$field = $_POST["stream"];
$contact = $_POST["mobile"];
$eaddress = $_POST["email"];
$institute = $_POST["college"];
$naukri = $_POST["job"];
$query = mysql_query("insert into students(fname, lname, gender, stream, mobile, email, college, job)
values ('$name', '$last_name', '$sex', '$field','$contact', '$eaddress', '$intitute', '$naukri')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
mysql_close($connection); // Closing Connection with Server
?>
After running; In the inspect element I checked the response:- It shows Data Inserted successfully but actually an empty row is getting inserted. Basically what i think I am not able to correctly grab the data properly from form. Can somebody please check what is the problem. It will be a great help.
The attribute is method, not type. This typo is causing your form to process a GET rather than a POST. So all your variable assignments are wrong.
$first_name = $_POST["fname"];
would be
$first_name = $_GET["fname"];
or you could use the $_REQUEST; or you can just correct the attribute,
<form action="register.php" id="contactForm" method="post">
Your code also is wide open to SQL injections and is using the deprecated mysql_ functions. You should update to mysqli or pdo and be using prepared statements with parameterized queries.
More on SQL injections:
http://php.net/manual/en/security.database.sql-injection.phpHow can I prevent SQL injection in PHP?https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet#Defense_Option_1:_Prepared_Statements_.28Parameterized_Queries.29

How to let a button automatically enter values from the database into a form?

This is an image of how the page looks like,
The 2 big blue buttons at the top are buttons that represent a value from the database. The main function of this button is to help the user to fill in the form in a convenient way leaving only the "Description" to be filled in.
This is my code for the page,
<div class="bodycontainer">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">My Records</h3>
</div>
<div class="panel-body">
<?php
require 'dbfunction.php';
$con = getDbConnect();
$day = date("l");
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM timetableschedule WHERE day='" . $day . "'");
while ($schedule = mysqli_fetch_array($result)) {
?>
<div class="col-md-4">
<div class="admininfobox">
<a class="btn btn-primary">
<?php
echo "<br/>";
echo $schedule['academicInstitution'] . "<br />";
echo $schedule['startTime'] . "-" . $schedule['endTime'] . "hrs<br />";
echo "<br/>";
?>
</a>
</div>
</div>
<?php
}
mysqli_close($con);
}
?>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Record Activity</div>
<div class="panel-body">
<form name="Create New Admin" class="form-horizontal" method="post" action="handlerecord.php">
<div class="form-group">
<div class="col-sm-4">
<label>Academic Institution</label>
<input list="AcadInst" type="text" class="form-control" placeholder="Institution Name" name="academicInstitution">
<datalist id="AcadInst">
<option value="Singapore Polytechnic (SP)">
<option value="Ngee Ann Polytechnic (NP)">
<option value="Temasek Polytechnic (TP)">
<option value="Republic Polytechnic (RP)">
<option value="Nanyang Polytechnic (NYP)">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Level of Teaching</label>
<input list="LvTeaching" type="text" class="form-control" placeholder="Teaching Stage" name="levelofteaching">
<datalist id="LvTeaching">
<option value="Undergraduate Teaching">
<option value="Postgraduate Teaching">
<option value="Continuing Education">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Type of Teaching</label>
<input list="TyTeaching" type="text" class="form-control" placeholder="Teaching Type" name="typeofteaching">
<datalist id="TyTeaching">
<option value="Clinical Teaching">
<option value="Academic Teaching">
<option value="Talk">
<option value="Others (Please specify)">
</datalist>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<label for="startdate">Start Time</label>
<input type="text" class="form-control" placeholder="Select Time" name="starttime">
</div>
<div class="col-sm-4">
<label for="enddate">End Time</label>
<input type="text" class="form-control" placeholder="Select Time" name="endtime">
</div>
<div class="col-sm-4">
<label for="enddate">Description</label>
<input type="text" class="form-control" placeholder="Optional" name="Description">
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<input type="submit" value="Add" class="btn btn-primary">
</input>
</div>
</div>
</form>
</div>
</div>
An overview of the database,
I am not sure on how to code it. Like Once the user click onto the button, the button goes through the database and input the data accordingly into the form ready for the user to submit.
My suggestion is to create an AJAX request on click.
Create a page loadFormData.php
Accept key data to search in database. For eg: id. Then search in database with this key and get the results. Finally encode this to json (json_encode()) and echo the result.
When the user clicks on the button, trigger an ajax (Javascript) function and send the key values to the loadFormData.php
From the returned json result, extract institution name, start time, end time etc and fill this to form fields using JavaScript
$("#blueButton").click(function() {
$.ajax({
type: "GET",
url: "loadFormData.php",
dataType: "json",
success : function(data) {
// extract each item from the variable 'data'
}
});
});
You should create a form and use action to state the method of entering the values that you want.
For example
http://www.w3schools.com/php/php_forms.asp
You can use this to enter the data easily.

Bootstrap - Custom Form

I am using Bootstrap. I am trying to create a working form, but the submit function doesn't work.
Here is my form HTML:
<form class="form-horizontal" role="form" action="mph.php" method="POST">
<div class="modal-body">
<fieldset>
<!-- Form Name -->
<legend>Map Problem Reporter</legend>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">What map?</label>
<div class="col-md-6">
<select id="selectbasic" name="selectbasic" class="form-control">
<option value="1">6 Stages of Parkour</option>
<option value="2">20 Stages of Parkour</option>
</select>
</div>
</div>
<!-- Multiple Radios -->
<div class="form-group">
<label class="col-md-4 control-label" for="radios">What kind of problem?</label>
<div class="col-md-4">
<div class="radio">
<label for="radios-0">
<input type="radio" name="radios" id="radios-0" value="1">
Blocks
</label>
</div>
<div class="radio">
<label for="radios-1">
<input type="radio" name="radios" id="radios-1" value="2">
Commands / Command Blocks
</label>
</div>
<div class="radio">
<label for="radios-2">
<input type="radio" name="radios" id="radios-2" value="">
Entities / Items
</label>
</div>
<div class="radio">
<label for="radios-3">
<input type="radio" name="radios" id="radios-3" value="">
Other (describe in the next text field)
</label>
</div>
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Describe the Problem</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea" name="textarea"></textarea>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">What is the priority?</label>
<div class="col-md-6">
<select id="selectbasic" name="selectbasic" class="form-control">
<option value="1">Huge! It breaks the map!</option>
<option value="2">Big</option>
<option value="">Noticeable</option>
<option value="">Barely Noticeable</option>
<option value="">Almost Hidden</option>
<option value="">I do not know</option>
</select>
</div>
</div>
<input type="submit" name="submit" value="Save Data">
</fieldset>
</form>
And here is my current PHP:
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
$ret = file_put_contents('mpr.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
?>
When I click "Save Data" in the form, it doesn't update my mpr.txt with the information. Please help.
I see 2 problems:
1) You have created 2 forms with
<form class="form-horizontal" role="form">
and
<form class="form-horizontal">
which should be 1
and
2) You are missing the action attribute in <form> tag i.e. it should be <form class="form-horizontal" role="form" action="<php file name>" method="<request type>">
correct these and I think you should be through.
You do not see the data in your .txt file because you do not have any field named field1 and field2 in your html page. The field names have to match in both html and php files to write/read the data correctly. Please correct the names and you should be through.
Also would appreciate if you accept the answer.
The answer is quit obvious! PHP is trying to find field1 and field2 posted. But in your HTML form you did not define these two properties, consequently PHP won't update the file!
EDIT:
If you submit the form and print_r($_POST) you will get the follow array:
Array ( [selectbasic] => 1 [textarea] => [submit] => Save Data )
As you see, there is no [field1] or [field2] in the response!
Which means PHP cannot proceed to to open and update the text file!
To prevent this, you should define field1 and field2 in your HTML form by adding these input tags or updating the existing ones.
For instance, if you are interested in the select tag and the textarea tag, you should whether update you HTML code or you php code.
You could go with PHP code by updating your if statement into if(isset($_POST['selectbasic'], $_POST['textarea'])) or in your HTML form change the name="selectbasic" into name="field1" and name="textarea" into name="field2 and keep your current php code and so your current PHP if statement that says: if(isset($_POST['field1']) && isset($_POST['field2']))
I hope this explains it all!

Categories