is it possible for me to do an auto increment number in a sequential text that can be display in PHP and HTML, for example;
SKP/SH/1(AUTO_INCREMENT)/2017
SKP/SH/2/2017
Only the number is increase, and the SKP/SH/""/2017 is fixed.
Thanks in advance, appreciate your help!
side note: I already set primary key and auto increment in mysql (separated with 2 column for ID and CODE)
HTML:
<form name=b onsubmit="return check()" action="form.php" method="post">
Nama Program: <br />
<textarea required name="program" cols="50" rows="10"></textarea><br />
<p></p>
Tarikh Program Mula: <br />
<input required name="tarikhmula" type="text" class="datepicker">
<p></p>
Tarikh Program Tamat: <br />
<input required name="tarikhtamat" type="text" class="datepicker">
<p></p>
Sebut Harga Bilangan: <br />
<p></p>
Kod Sebut Harga: <br />
<input required name="kodsebutharga" type="text" value="SKP/SH/2017" />
<p></p>
Tarikh Iklan Dibuka: <br />
<input required name="iklanmula" type="text" class="datepicker">
<p></p>
Tarikh Iklan Ditutup: <br />
<input required name="iklantamat" type="text" class="datepicker">
<p></p>
Nama Pegawai Penyelaras: <br />
<input required name="namapegawai" type="text" />
<p></p>
Anggaran Harga Jabatan: <br />
<input required name="anggaranharga" type="text" id="number" pattern="[0-9]*">
<p></p>
<input name="" type="submit" /><input name="" type="reset" value="Clear" />
</form>
</body>
</html>
PHP:
<?php
require("config.php");
$link =mysqli_connect($h,$u,$p,$db);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
error_reporting(E_ALL ^ E_DEPRECATED);
$query = "insert into atas50 (program, tarikhmula, tarikhtamat, namapegawai, anggaranharga, iklanmula, iklantamat) values ('$_POST[program]','$_POST[tarikhmula]','$_POST[tarikhtamat]','$_POST[namapegawai]','$_POST[anggaranharga]','$_POST[iklanmula]','$_POST[iklantamat]')";
if (mysqli_query($link, $query)) {
//echo "New record created successfully";
$message = "New record created successfully";
echo "<script type='text/javascript'>alert('$message');</script>";
header( "refresh:0; url=view.php" );
} else {
echo "Error: " . $query . "<br>" . mysqli_error($link);
}
mysqli_close($link);
?>
Something like this should work (pseudo-code):
$counter = 1;
while ($row = fetch_from_database()) {
echo 'SKP/SH'+$counter+'/2017';
$counter++;
}
Note that this is entirely pseudo-code, and you'll have to implement it as you need.
Related
Im having some trouble uploading an image with php to phpmyadmin.
Here is my markup so far.
<form method="post">
<p>Event Date<span> - eg "23" for the 23rd</span><br /> <input type="text" name="eventDate" maxlength="2" required /> </p>
<p>Event Month<span> - eg "Mar" for March</span> <br /><input type="text" name="eventMonth" maxlength="3" required /> </p>
<p>Artist Image <br /><input type="file" name="artistImage" required /> </p>
<p>Artist Name <br /><input type="text" name="artistName" maxlength="30" required /> </p>
<p>Location <br /><input type="text" name="location" maxlength="30" required /> </p>
<p>Price <br /><input type="text" name="price" maxlength="10" required /> </p>
<p>Time <br /><input type="text" name="time" maxlength="10" required /> </p>
<input id="eventSubmit" type="submit" name="submitEvent" />
</form>
and here is my php code
`
if(isset($_POST['submitEvent'])){
$addEventDate = $_POST['eventDate'];
$addEventMonth = $_POST['eventMonth'];
$addEventName = $_POST['artistName'];
$addEventLocation = $_POST['location'];
$addEventPrice = $_POST['price'];
$addEventTime = $_POST['time'];
$addEventImg = $_FILES['artistImage']['name'];
$temp_name1 = $_FILES['artistImage']['tmp_name'];
move_uploaded_file($temp_name1, "img/$addEventImg");
$ticket= 1;
$insert_product = "INSERT INTO events (eventDate,eventMonth,artistImg,artistName,location,price,time,buyTicket) VALUES ('$addEventDate','$addEventMonth','$addEventImg','$addEventName','$addEventLocation','$addEventPrice','$addEventTime','$ticket')";
$run_product = mysqli_query($con,$insert_product);
echo mysqli_error($con);
if($run_product){
echo "<script>alert('Product has been inserted.');</script>";
echo "<script>window.open('admin.php','_self');</script>";
}
}
?>`
I have set the database up to take the image as a blob here is a picture of my database
and this is the code that is rendering the products onto the events page.
<?php
$get_events = "select * from events";
$run_events = mysqli_query($con,$get_events);
while($row_events = mysqli_fetch_array($run_events)){
$eventDate = $row_events['eventDate'];
$eventMonth = $row_events['eventMonth'];
$artistImg = $row_events['artistImg'];
$artistName = $row_events['artistName'];
$location = $row_events['location'];
$price = $row_events['price'];
$time = $row_events['time'];
$buyTicket = $row_events['buyTicket'];
echo "
<div class='eventBox'>
<div class='dateBanner'>
<p><span>$eventDate</span><br />$eventMonth</p>
</div>
<img src='img/$artistImg' alt='event image' />
<div class='eventDetails'>
<h2>$artistName</h2>
<p><span>Location</span> <br /> $location<br /><br /><span>Ticket Price</span> <br /> £$price<br /><br /><span>Time</span> <br />$time<br /><br /></p>
</div>
<div class='PurchaseTicket'>
<h3>Buy Ticket</h3>
</div>
</div>
";
}
?>
This works for everything apart from the image. The image just displays the alt tag not the actual image. It also doesn't save the image in the folder either.
If anyone could help I'd really appreciate it!!
Thanks
Using php, i created an edit option that basically lets u change the data in the record. However, after clicking on submit, the data doesn't get saved on the table nor do i get any errors.. I am quite confused. Here r my codes:
<html>
<body>
<?php
include('db.php');
if (isset($_GET['Id'])) {
$Id=$_GET['Id'];
if (isset($_POST['submit'])) {
$Fname=$_POST['Fname'];
$Lname=$_POST['Lname'];
$Age=$_POST['Age'];
$Nationality=$_POST['Nationality'];
$PhoneNumber=$_POST['PhoneNumber'];
$Email=$_POST['Email'];
$query3=mysql_query("UPDATE `students` SET `Fname`='$Fname',`Lname`='$Lname',`Age`='$Age',`Nationality`='$Nationality',`PhoneNumber`='$PhoneNumber',`Email`='$Email' WHERE `Id`='$Id'");
if($query3) {
header('location:index1.php');
}
}
$query1=mysql_query("select * from students where Id='$Id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="">
First Name:<input type="text" name="name" value="<?php echo $query2['Fname']; ?>" /><br />
Last Name:<input type="text" name="name" value="<?php echo $query2['Lname']; ?>" /><br />
Age:<input type="text" name="age" value="<?php echo $query2['Age']; ?>" /><br />
Nationality:<input type="text" name="name" value="<?php echo $query2['Nationality']; ?>" /><br />
Phone Number:<input type="text" name="name" value="<?php echo $query2['PhoneNumber']; ?>" /><br />
Email:<input type="text" name="name" value="<?php echo $query2['Email']; ?>" /><br />
<br />
<input type="submit" name="submit" value="update" />
</form>
<?php
}
?>
</body>
</html>
I'm trying to use both $_GET and $_POST in an sql query. The following is my code:
<?php
$assignment = mysql_real_escape_string($_GET['name']);
echo "$assignment <br>";
if (isset($_POST['add'])) {
$user = $_POST['username'];
$text = $_POST['comment'];
$query = "INSERT INTO comments (user, text, assignment) VALUES ('$user', '$text', '$assignment')";
mysql_query($query) or die('Error, comment failed to post');
}
?>
<h1>Add Comment</h1>
<form action="log_entry.php" method="post">
Name:<br/>
<input type="text" name="username" value="" />
<br /><br />
Comment:<br />
<textarea style="height:200px;" type="text" name="comment" value="" ></textarea>
<br /><br />
<input type="submit" name="add" value="Add Comment" />
</form>
However, the $assignment variable does not work in the query. It is echoed properly before the query is made but its value inside the table after the INSERT is completed is empty. What exactly is causing this?
Instead of trying to combine GET and POST, use a hidden input field:
<?php
$assignment = mysql_real_escape_string($_POST['name']); // Name is now in POST data, so swap this
echo "$assignment <br>";
if (isset($_POST['add'])) {
$user = $_POST['username'];
$text = $_POST['comment'];
$query = "INSERT INTO comments (user, text, assignment) VALUES ('$user', '$text', '$assignment')";
mysql_query($query) or die('Error, comment failed to post');
}
?>
<h1>Add Comment</h1>
<form action="log_entry.php" method="post">
<!-- Add hidden input to carry the name -->
<input type="hidden" name="name" value="<?php echo $_GET['name']; ?>"/>
<!-- Rest of the form is the same -->
Name:<br/>
<input type="text" name="username" value="" />
<br /><br />
Comment:<br />
<textarea style="height:200px;" type="text" name="comment" value="" ></textarea>
<br /><br />
<input type="submit" name="add" value="Add Comment" />
</form>
I have two php files, form and the results page. Form page accepts user info and the result pages inserts the information into the database and displays it.
form page code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Record Insertion Form</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
echo "<div style='font-size: large; font-family: sans-serif'><center><h1>
<p style='color: white; background-color: black'>Resume Generator</p></h1></center> </div>";
echo "Mandotary fields with <font color='red'>*</font> are required!";
?>
<form id="form1" name="form1" method="post" action="resumeinsert.php">
<fieldset>
<legend>Personal Details</legend>
<input type="hidden" name="id" id="id" />
<br class="clear" />
<label for="name"><font color="red">*</font> Name:</label><input type="text" name="name" id="name" />
<br class="clear" />
<label for="gender"><font color="red">*</font> Gender:</label>
<input type="radio" name="gender" value="Male" id="gender_0" />Male
<input type="radio" name="gender" value="Female" id="gender_1" />Female
<br class="clear" />
<label for="dateofbirth"><font color="red">*</font> Date of birth:</label><input type="text" name="dateofbirth" id="dateofbirth" />
<br class="clear" />
<label for="placeofbirth"><font color="red">*</font> Place of birth :</label><input type="text" name="placeofbirth" id="placeofbirth" />
<br class="clear" />
<label for="address"><font color="red">*</font> Address:</label><textarea name="address" id="address" cols="45" rows="5"></textarea>
<br class="clear" />
</fieldset>
<fieldset>
<legend><b>Educational Details</b></legend>
<label for="schoolname"><font color="red">*</font> School name:</label><input type="text" name="schoolname" id="schoolname" size=50 />
<br class="clear" />
<label for="qualification"><font color="red">*</font> Qualification(s):</label><textarea name="qualification" id="qualification" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="skills1"><font color="red">*</font> Skills 1:</label><textarea name="skills1" id="skills1" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="skills2">Skills 2:</label><textarea name="skills2" id="skills2" cols="45" rows="5"></textarea>
<br class="clear" />
</fieldset>
<fieldset><legend><b>Other Details</b></legend>
<label for="awards">Awards:</label><textarea name="awards" id="awards" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="Volunteer">Volunteer:</label><textarea name="Volunteer" id="Volunteer" cols="45" rows="5"></textarea>
<br class="clear" />
<input type="submit" name="submit" id="submit" value="Submit" />
<br class="clear" />
</fieldset>
</form>
</body>
</html>
resumeinsert.php page code:
<?php
$mysqli = mysqli_connect("localhost", "hecton", "ccna", "joomladb");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
$gender = $_POST['gender'];
$dateofbirth = $_POST['dateofbirth'];
$placeofbirth = $_POST['placeofbirth'];
$address = $_POST['address'];
$schoolname = $_POST['schoolname'];
$qualification = $_POST['qualification'];
$skills1 = $_POST['skills1'];
$skills2 = $_POST['skills2'];
$awards = $_POST['awards'];
$Volunteer = $_POST['Volunteer'];
$query = " INSERT INTO j71mi_resumeinfo ( id, name, gender, dateofbirth, placeofbirth, address, schoolname, qualification, skills1, skills2, awards, Volunteer ) VALUES ( '$id', '$name', '$gender', '$dateofbirth', '$placeofbirth', '$address', '$schoolname', '$qualification', '$skills1', '$skills2', '$awards', '$Volunteer' ) ";
$result = mysqli_query($mysqli, $query);
if ($result === TRUE) {
echo "A record has been successfully inserted into the database!.";
echo "<b><h1><center>My Resume</h1></b></center>";
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Personal Details</p></div>";
echo "<br>";
echo "<b>Name:</b>".$_POST['name'];
echo "<br>";
echo "<b>Gender:</b>".$_POST['gender'];
echo "<br>";
echo "<b>Date of Birth:</b>".$_POST['dateofbirth'];
echo "<br>";
echo "<b>Place of Birth:</b>".$_POST['placeofbirth'];
echo "<br>";
echo "<b>Home Address:</b>".$_POST['address'];
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Educational Details</p></div>";
echo "<br>";
echo "<b>School Name:</b>".$_POST['schoolname'];
echo "<br>";
echo "<b>Qualification(s):</b>".$_POST['qualification'];
echo "<br>";
echo "<b>Skill 1:</b>".$_POST['skills1'];
echo "<br>";
echo "<b>Skill 2:</b>".$_POST['skills2'];
echo "<br>";
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Other Details</p></div>";
echo "<br>";
echo "<b>Award(s):</b>".$_POST['awards'];
echo "<br>";
echo "<b>Volunteer Activity:</b>".$_POST['Volunteer'];
echo "<br>";
echo "<br>";
echo "<b><h5><center>End of Resume</h5></b></center>";
} else {
printf("Could not insert record: %s\n", mysqli_error($mysqli));
}
mysqli_close($mysqli);
}
?>
My problem is that user sometimes submit the form without even filling out all the fields and it displays " Name: ", which contains nothing.
How can I make the users to fill out the missing required fields after they click submit button with a red text next to the missing fields ? So that empty fields are not sent to database.
2.Some fields are set to NULL in the database, which means the field is optional. How can I not display that specific field in display page if user enters nothing in the form?
You have to validate the form. Use simple checks to determine if fields are filled correctly. Check for empty, alphanumeric, and what ever else you need. It would probably be best to check in the front end before you send the data over to the server. Here are some examples to get you started:
JS Form validation
http://www.w3schools.com/js/js_form_validation.asp
JS Form validation
http://www.tizag.com/javascriptT/javascriptform.php
PHP Form validation
http://www.w3schools.com/php/php_form_validation.asp
Check out these examples and implement something similar to this. Even if you end up validating the form in JS don't forget to implement the bare minimum checks in PHP.
i want to store an image in database not by inserting but by updating (by using UPDATE query).
Everything else is updated successfully but its not storing image. I'm using medium blob data type to store image.
Here is my code:
update-profile.php:
<form id="form" method="post" action="update-profile-action.php" enctype="multipart/form-data">
<label for="Fname">First Name:</label> <input type="text" id="Fname" class="text" value="<?php echo $firstname; ?>" name="Fname" /> <br /><br />
<label for="Lname">Last Name:</label> <input type="text" id="Lname" class="text" value="<?php echo $lastname; ?>" name="Lname" /><br /> <br />
<?php
if ($_SESSION["type"]=="T")
{
?>
<label>Profile Image:</label> <input type="file" name="image" value="" /><br /> <br />
<label>Qualification:</label><textarea name="qualification" class="text" id="qualification"><?php echo $qualification;?></textarea><br /><br />
<label>Education & Teaching History:</label> <textarea name="briefintro" class="text" id="intro"><?php echo $briefintro; ?></textarea><br /><br />
<?php
}
?>
<input type="submit" class="mybutton" value="Update Profile" />
</form>
update-profile-action.php:
<?php include("../includes/config.php");?>
<?php
$Fname=$_POST["Fname"];
$Lname=$_POST["Lname"];
$image=$_POST["profileimg"];
$briefintro=$_POST["briefintro"];
$qualification=$_POST["qualification"];
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $con);
$query=("UPDATE accounts SET firstname='".$Fname."' , lastname='".$Lname."' , profileimg='".$image."' , briefintro='".$briefintro."', qualification='".$qualification."' WHERE id=".$_SESSION['id']);
$result = mysql_query($query);
header("Location: update-profile.php?status=3");
mysql_close($con);
?>
i copied only the related data from update-profile.php to making it more easy to read :)
any kind of help will be appreciated :)
Two problems:
You do not have an id for the input tag for the file. You need to change <input type="file" name="image" value="" /> to <input type="file" name="image" value="" id = "profileimage" />
Secondly you access files not via $_POST but rather via $_FILES.