i want to update value(s) as a student change value(s) in correction or update form.
till know i'm able to fetch and display values in text boxes on the bases of name selected from dropdown list from data base using ajax and json. but when i try to update database it do not works...
HTML:
<select name="u_stu" id="u_stu" onchange="show(this.value);" style="float:right; height:30px; width:180px;">
<option selected="selected" disabled="disabled">Choose</option>
<option>stu1</option>
<option>stu2</option>
<option>stu3</option>
</select>
name: <input type="text" id="name" name="name" style="float:right; height:20px; width:200px;"/><br /><br />
age: <input type="text" id="age" name="age" style="float:right; height:20px; width:200px;" /><br /><br />
phone: <input type="text" id="phone" name="u_ver_txt" style="float:right; height:20px; width:200px;" /><br /><br />
address: <input type="text" id="add" name="add" style="float:right; height:20px; width:200px;" /><br /><br />
hobby: <input type="text" id="hobby" name="hobby" style="float:right; height:20px; width:200px;" /><br /><br />
<input type="submit" value="Submit" name="u_s2" id="u_s2" style="position:relative; top:-180px; "/>
MYSQL PHP
<?php
$c=mysql_connect("localhost","abc","xyz");
mysql_select_db("root");
if(isset($_POST['u_s2']))
{
$name=$_POST["name"];
$age=$_POST["age"];
$phone=$_POST["phone"];
$address=$_POST["address"];
$hobby=$_POST["hoddy"];
$id=$_POST["u_id"];
$q2="UPDATE student SET
name=$name,age=$age,phone=$phone,address=$address,hobby=$hobby WHERE Sr. no=$id";
mysql_query($q2);
}
?>
You need something like this:
$q= "update student set name = '".$name."', age = '".$age."', phone = '".$phone."', address = '".$address."', hobby = '".$hobby."' WHERE user = 'the user id'";
You should use a WHERE statement as well like the above example so that you can be sure that you are updating the correct row.
You should also consider using mysql_real_escape_string function:
$name = mysql_real_escape_string($_POST['name']);
http://php.net/manual/en/function.mysql-real-escape-string.php
If this will be an updating form you should also include the value in the input fields and etc. so they will see the values and update what they need.
I also suggest that you use mysqli functions instead of mysql functions as mysql is no longer supported and deprecated.
Related
I have a Form with in php. I have it talking and inserting to the the MYsql database perfectly if i have all fields in the form filled in. Basically i have 3 text fields and 2 dates fields. i only want to chose either date field instead of both.
I have tried only inserting 1 date and it does not insert in to DB only when i have both dates chosen it works.
Below is the code which works when both date fields are chosen. i want to be able to only have 1 or the other or both if possible
<?php
require('db.php');
//Inserting Data Variables
if(isset($_POST['submit']))
{
echo $owner = $_POST['owner'];
echo $budget = $_POST['budget'];
echo $status = $_POST['status'];
echo $s_jan = $_POST['s_jan'];
echo $s_feb = $_POST['s_feb'];
//Query the database
$query = "insert into sbook(owner,budget,status,s_jan,s_feb)
values('$owner','$budget','$status','$s_jan','$s_feb')";
$insert_query = mysqli_query($con, $query);
}
?>
Here is the form
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
<label>Owner</label>
<br />
<input class="input" type="text" name="owner">
<br />
<label>EST Budget:</label><br />
<input class="input" type="text" name="budget"/>
<br />
<label>Status:</label><br />
<input class="input" type="text" name="status"/>
<br />
<label>Jan</label><br />
<input class="input" type="date" name="s_jan"/>
<br />
<label>Feb</label><br />
<input class="input" type="date" name="s_feb"/>
<br />
<p>
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Above is the code which works when both date fields are chosen. i want to be able to only have 1 date or the other date or both if possible
I'm trying to all users to add data to a database. However, it's not working and I'm not getting an error. This is purely for practice, but it's still frustrating. Thoughts?
<form name="Add" id="Add" method="post" action="programadd.php">
<p>Content Name:
<input name="program" type="text" id="program" style="width: 500px; height: 20px;" />
</p>
<p>Content Air Date
<input name="airdate" type="date" id="airdate" />
</p>
<p>Description
<input name="description" type="text" id="description" style="width: 500px; height: 20px;" />
</p>
<p>Production
<input name="production" type="text" id="production" value="nothing" style="width: 500px; height: 20px;" />
</p>
<p>Promotions
<input name="promotion" type="text" id="promotion" value="nothing" style="width: 500px; height: 20px;" />
</p>
<p>Community
<input name="community" type="text" id="community" value="nothing" style="width: 500px; height: 20px;" />
</p>
<p>Web
<input name="web" type="text" id="web" value="nothing" style="width: 500px; height: 20px;" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
Here's the programadd page. Thanks!
<?php
include('connect-db.php');
$program = $_POST['program'];
$airdate = $_POST['airdate'];
$description = $_POST['description'];
$production = $_POST['production'];
$promotion = $_POST['promotion'];
$community = $_POST['community'];
$web = $_POST['web'];
if (mysql_query ("INSERT INTO calendar(program, airdate, description, production, promotion, community, web) VALUES
('$program', '$airdate', '$description','$production', '$promotion', '$community', '$web')"))
{ echo "Content successfully added to the database. <br />
}
else {
die(mysql_error());
}
require_once("db_connx_close.php");
?>
{ echo "Content successfully added to the database. <br />
^---missing " here
If you're NOT getting a syntax error from this, then you're probably running with display_errors and error_reporting turned off. NEVER have them disabled when you're developing. It's the coding equivalent of stuffing your fingers in your ears and going "lalalala can't hear you don't care what you have to tell me lalala".
This may be an error in your question's markdown, but just incase it isn't I will mention what I am seeing.
You are not closing the string on the echo after your query:
echo "Content successfully added to the database. <br />
Should be:
echo "Content successfully added to the database. <br />";
Also, just for the sake of clarity, you are using $_POST data directly in a SQL Query without sanitizing or validating the data. I realize this is for practice but it should be mentioned that this methodology is highly vulnerable to SQL Injection and will make your database easily accessible. You could, instead, use PHP's PDO Library for your connections or at the very least sanitize your inputs.
However your code is vulnereble to sql-injection although for your help put this change in your query syntax like this--
if (mysql_query ("INSERT INTO calendar(program, airdate, description, production, promotion, community, web) VALUES ('".$program."', '".$airdate."', '".$description."','".$production."','".$promotion."', '".$community."', '".$web."')"))
{
echo "Content successfully added to the database. <br />";
}
So I have a form that users fill out and it adds to a database. Everything works except for the date. The date shows up as 0000-00-00. Currently, I'm using a datepicker, but the date wasn't showing up even when the user had to type it in...so that shouldn't be the issue.
<form name="Add" id="Add" method="post" action="programadd.php">
<p>Program Name:
<input name="program" type="text" id="program" />
</p>
<p>Air Date
<input name="airdate" type="text" id="airdate" />
</p>
<p>Description
<input name="description" type="text" id="description" s />
</p>
<p>Production
<input name="production" type="text" id="production" />
</p>
<p>Promotions
<input name="promotion" type="text" id="promotion" />
</p>
<p>Community
<input name="community" type="text" id="community" />
</p>
<p>Web
<input name="web" type="text" id="web" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
And here's the code that adds it to the database:
<?php require_once('connect-db.php');
$program = $_POST['program'];
$airdate = $_POST['airdate'];
$description = $_POST['description'];
$production = $_POST['production'];
$promotion = $_POST['promotion'];
$community = $_POST['community'];
$web = $_POST['web'];
if (mysql_query ("INSERT INTO calendar(program, airdate, description, production, community, promotion, web) VALUES
('$program', '$airdate', '$description','$production', '$promotion', '$community', '$web')"))
{ echo "Program successfully added to the database <br />";
}
else {
die(mysql_error());
}
require_once("db_connx_close.php");
?>
One other question, how do I get the date to display as Month/Date/Year, instead of the reverse?
$date = date('Y-m-d', strtotime($_POST['airdate']));
Ik want to prevent field reset after validation. I have searched some topics but i cant implement it in my code.
if($form_view == true) {
echo '';
if($error != '') {
echo '<strong><font color="#FF0000">Fout:</font></strong><br />'.$error.'<br />';
}
echo '<form method="POST" action="'.$filename.'" style="border:0px; margin:0px; padding:0px;">
Voornaam
<br />
<input type="text" name="voornaam" maxlength="50" id="input_contact" style="width: 200px; value="'.(isset($_POST['voornaam']) ? $_POST['voornaam'] : '').'">
<br />
Achteraam
<br />
<input type="text" name="achternaam" maxlength="50" id="input_contact" style="width: 200px; value="'.(isset($_POST['achternaam']) ? $_POST['achternaam'] : '').'">
<br />
Adres
<br />
<input type="text" name="adres" maxlength="50" id="input_contact" style="width: 400px; value="'.(isset($_POST['adres']) ? $_POST['adres'] : '').'">
<br />
Postcode <h7><i><small>(1234 AB)</small></i></h7>
<br />
<input type="text" name="postcode" maxlength="7" id="input_contact" style="width: 100px; value="'.(isset($_POST['postcode']) ? $_POST['postcode'] : '').'">
<br />
Woonplaats
<br />
<input type="text" name="woonplaats" maxlength="50" id="input_contact" style="width: 200px; value="'.(isset($_POST['woonplaats']) ? $_POST['woonplaats'] : '').'">
<br />
Telefoonnummer <h7><i><small>(0123-456789)</small></i></h7>
<br />
<input type="text" name="telefoonnummer" maxlength="11" id="input_contact" style="width: 100px; value="'.(isset($_POST['telefoonnummer']) ? $_POST['telefoonnummer'] : '').'">
<br /><br />
If you can give me one example on how to implement this in one field.
Thanks a lot
If the post is not made to this specific page they're landing on then the $_POST variable will not contain that information.
The quick and dirty of it is to save the information submitted in the $_SESSION[] as an array.
When the form is submitted, in addition to doing whatever you're currently doing, you need to save the information submitted into the session on the receiving script.
if ($_POST['submit']) {
// repeat or configure as desired to save submitted fields into Session
$_SESSION['form_info']['email_address'] = $_POST['email_address'];
}
On the form page itself you would use the following
if (isset($_SESSION['form_info'])) {
// You'll want to most likely consider filtering these using appropriate functions.
<input type="text" name="email_address" maxlength="50" id="email_address" style="width: 200px; value="<?php if (isset($_SESSION['form_info']['email_address']) { $_SESSION['form_info']['email_address']; } ?>">
}
Is the form action variable $filename referring to this particular php file or somewhere else where validation happens? If the form action happens somewhere else, the Moylin's answer is your solution (using $_SESSION). Otherwise, if possible, it's reasonable and simple to do the validation right here. echoing $_POST to value fields, as you did, should be sufficient enough (you dont even need the ternary operation there, if you want short code).
<form method="POST" action="thisPhpFile.php" >
value ="'. #$_POST['voornaam'] .'"
In the validation part (before echoing the form), you naturally want to check for all the errors, required fields etc. If everything is valid just point user to wanted location. Something along these lines:
if(isset($_POST['mySubmit'] {
//check errors/validate
if($valid == true) {
Header("Location: yourLocation.php");
}
}
else {
//echo your form here
}
First of all thanks in advance, this has been very frustrating and I'm hoping someone can see something I'm not, I am definitely no php expert. Well here' what is going on.
I have a form where I have a checkbox for people to opt in to our newletter. The form element looks like this:
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
That is then submitted to a php file with this code:
if (isset($_POST['newsletter']) && $_POST['newsletter'] == 'Yes'){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
$newsletter is then inserted into a database field.
The issue is that whether the box is checked or not it is being sent to php as true, so every entry is receiving the newsletter.
Any help would be greatly appreciated! Thanks!
Here's the full form minus the option list for the sake of brevity
<form method="post" action="contact.php" name="contactform" id="contactform">
<fieldset>
<legend>Please fill in the following form all fields are required, thanks!</legend>
<label for=firstName accesskey=F><span class="required">*</span>First Name</label>
<input name="firstName" type="text" id="firstName" size="30" value="" />
<br />
<label for=lastName accesskey=L><span class="required">*</span>Last Name</label>
<input name="lastName" type="text" id="lastName" size="30" value="" />
<br />
<label for=email accesskey=E><span class="required">*</span>Email</label>
<input name="email" type="text" id="email" size="30" value="" />
<br />
<label for=city accesskey=C><span class="required">*</span>City</label>
<input name="city" type="text" id="city" size="30" value="" />
<br />
<label for=state accesskey=S><span class="required">*</span>State</label>
<select name="state" type="text" id="state">
<option value="AL">Alabama</option>
...
<option value="WY">Wyoming</option>
</select>
<br />
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
<br />
<p><span class="required">*</span> Are you human?</p>
<label for=verify accesskey=V> 3 + 1 =</label>
<input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
Your code is correct. You most likely have a problem with your database insert/update logic. Why do you assume it is the PHP form handling?
This has just dawned on me.
If a checkbox is unchecked it isn't set in the $_POST superglobal. So if !isset($_POST['newsletter']) then it wasn't checked - if isset($_POST['newsletter']) it was checked.
Edit: Remove the 'yes' part - the value will never be yes, just true or 'on'.
Edit 2:
I've tested this to death. Change your code to:
if (isset($_POST['newsletter'])){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
Remove the value="Yes" attribute from your checkbox input also. If you want it checking by default use checked="checked".