this is going to be my second post, I am very confused and need some assistance. first I will explain what I would want it to do and then post my code. I am first search a database using first name, last name, or a date to print the results.
I first need help in only printing a repeating name once. It is printing every possible case and I would like it to match the fields (except arrival, reason, or even company (these can change ) )
Next once the results print on my search page, I would like radio buttons to be next to each set of data. for instance, if there are two people with the last name brown, i would like the first name, last name, and DL# to have ONE radio button next to it. resulting in two total buttons. this is where i am having trouble. once the selected radio is pressed and the next button (hyperlink) is pressed, i will then direct the user to a set of forms where the selected data ( firstname, lastname, dl#, company) is pre filled in the spots.
so all in all i need help limiting the prints of repeating individuals, and also i need assistance with saving data after a search function using a radio button to then print and pre populate the forms on the following page. my code currently for the search function is:
<h3>Search By First Name, Last Name, or Arrival Date (20XX-MO-DY)</h3>
<form action="searchpage.php" method="GET">
<label>Search:
<input type="text" name="searchname" id="searchname" />
</label>
<input type="submit" value="Search" />
</form>
My searchpage is :
<?php
$host = "localhost"; //server
$db = "practice_table"; //database name
$user = "root"; //databases user name
$pwd = ""; //password
mysql_connect($host, $user, $pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$searchTerm = trim($_GET['searchname']);
// Check if $searchTerm is empty
if ($searchTerm == "") {
echo "Enter name you are searching for.";
exit();
} else {
$sql = "SELECT * FROM contractor WHERE CONCAT(FIRSTNAME,' ',LASTNAME,' ', ARRIVAL) like
'%$searchTerm%'";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if (($count) >= 1) {
$output = "";
while ($row = mysql_fetch_array($query)) {
$output .= "First Name: " . $row['FIRSTNAME'] . "<br />";
$output .= "Last Name: " . $row['LASTNAME'] . "<br />";
$output .= "Arrival: " . $row['ARRIVAL'] . "<br />";
}
echo $output;
} else {
echo "There was no matching record for the name " . $searchTerm;
}
}
?>
this code above is where all the results print and I would like to regulate the repeating cases, also where the radio button per individual should be.
finally this is the form page the hyperlink goes to where I would like to pre poppulate the code.
Welcome Back Contractor. Please fill the following information in again below for today's visit.
First Name:
<input type="text" name="FIRSTNAME" id="FIRSTNAME" value="<?php echo $_POST['radio']; ?>"/>
Last Name:
<input type="text" name="LASTNAME" id="LASTNAME" value="<?php echo $_POST['radio']; ?>"/>
<form action="insert_submit.php" method="post" style="margin-left:35px;">
First Name: <input type="text" name="FIRSTNAME" id="FIRSTNAME"/>
Last Name: <input type="text" name="LASTNAME" id="LASTNAME"/>
Purpose: <input type="text" name="PURPOSE" id="PURPOSE"/>
<br />
Company:
<input type="text" name="COMPANY" id="COMPANY"/>
DL #:
<input type="text" name="DRIVERL" id="DRIVERL"/>
<input type="radio" name="STATUS" id="STATUS" value="CHECKED IN">Log In
<br/>
<input type="radio" name="STATUS" id="STATUS" value="CHECKED OUT">Log Out
<br/>
<input type="submit" value="Submit">
<br/>
</form>
I appreciate any help. i hope I was just complicating things and can easily solve my last few issues. thank!
To remove duplicates from your results add DISTINCT or a GROUP BY clause to your SQL statment, eg:
SELECT DISTINCT FIRSTNAME, LASTNAME, PURPOSE FROM ...
or
SELECT DISTINCT FIRSTNAME, LASTNAME, PURPOSE
FROM ...
WHERE ...
GROUP BY DISTINCT FIRSTNAME, LASTNAME, PURPOSE
To add an arrival date while still only printing one row per person add MAX(ARRIVAL) to the SELECT clause:
SELECT DISTINCT FIRSTNAME, LASTNAME, PURPOSE, MAX(ARRIVAL)
FROM ...
WHERE ...
GROUP BY DISTINCT FIRSTNAME, LASTNAME, PURPOSE
Related
I have come to an obstacle and don't know how to proceed. In my html site user enter some data first data is dropdown box which is populated from number in database, so the user only selects here, second number you need to input and 3th number you need to input. 4th field is just random text for now.
What i would like to do is somehow multiply the number selected from dropdown box and the number which user inputs first and then save that number in a same row in which i save this input.
This is what i have so far:
HTML:
<form action="dodajanje_stevcev2.php" method="post">
Dropdown box: <select id="denominacija" type="number" step="0.001" name="denominacija" onchange="myFunction()">
//here is the php for dropdown box, which works fine
Enter 1st num: <input type="number" name="stava" /><br><br>
Enter 2nd num: <input type="number" name="dobitek" /><br><br>
Vrsta stave (please enter text): <input type="text"name="vrsta_stave" /><br><br>
</select>
<input type="submit", value="Poslji" />
</form>
PHP:
//here comes connection
echo "Connected successfully";
$total_in=$POST['denominacija'] * $POST['stava']; /this is where i am stuck
$sql = "INSERT INTO stevci (denominacija, stava, dobitek,vrsta_stave, total_in, date_time)
VALUES
('$_POST[denominacija]', '$_POST[stava]','$_POST[dobitek]','$_POST[vrsta_stave]','$total_in', CURRENT_TIMESTAMP() )";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
header('Location: dodajanje_stevcev.php'); exit;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}$conn->close();
?>
Hi found an answer myself:
$de = $_POST['denominacija'];
$st = $_POST['denominacija'];
$total_in=$de * $st;
I have searched across the entire web and I cant seem to find a solution. I am pretty much searching a database for individuals. I can use last name for instance and then if two people have the same last name they both will print on my page. I want to be able to select the individual I would want ( or am for instance ) and click next ( a hyperlink to a page with blank forms ) which is where I would like the info of the individual I selected ( and what was printed on the page ) in the forms pre filled. It allows a more user friendly approach then having to re type the data and also avoid miss spelling of company name or drivers license for example. I have put my code up many times but here it is again.this is the code for the finding and printing of the search results. no radio button yet, as i am trying to figure that out.
<div id="results" style="width:750px;height:225px; text-align: center">
<?php
$host = "localhost"; //server
$db = ""; //database name
$user = ""; //databases user name
$pwd = ""; //password
mysql_connect($host, $user, $pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$searchTerm = trim($_GET['searchname']);
// Check if $searchTerm is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
else
{
//$sql = "SELECT * FROM contractor WHERE CONCAT(FIRSTNAME,' ',LASTNAME,' ',
ARRIVAL) like '%$searchTerm%' GROUP BY FIRSTNAME";
$sql= "SELECT * FROM contractor WHERE CONCAT(FIRSTNAME,' ',LASTNAME,' ', ARRIVAL)
like '%$searchTerm%'";
$query = mysql_query($sql);
$count=mysql_num_rows($query);
//array_unique($count);
if(($count)>=1)
{
$output = "";
while($row = mysql_fetch_array($query))
{
$output .= "First Name: " . $row['FIRSTNAME'] . "<br />";
$output .= "Last Name: " . $row['LASTNAME'] . "<br />";
$output .= "Arrival: " . $row['ARRIVAL'] . "<br />";
}
echo $output;
//echo array_unique($output);
}
else
echo "There was no matching record for the name " . $searchTerm;
}
?>
</div>
this is the code for my form field which of course is a empty form that is just being submitted to the database after submission.
<form action="insert_submit.php" method="post" style="margin-left:35px;">
First Name: <input type = "text" name="FIRSTNAME" id="FIRSTNAME" />
Last Name: <input type = "text" name="LASTNAME" id="LASTNAME"/>
Purpose: <input type = "text" name="PURPOSE" id="PURPOSE"/>
<br>
</br>
Company: <input type = "text" name="COMPANY" id="COMPANY" />
DL #: <input type = "text" name="DRIVERL" id="DRIVERL" />
<br>
</br>
<input type="radio" name="STATUS" id="STATUS" value="Checked In">Log In
<br></br>
<input type="radio" name="STATUS" id="STATUS" value="Checked Out">Log Out
<br></br>
<input type="submit" value="Submit" >
<br>
</br>
</form>
so your output line would be something like (clean up to suit your formatting)
$output .= '<a href="'.$_SERVER['PHP_SELF'].'?userid='.$row['UNIQUEID'].'">
First Name: '.$row['FIRSTNAME'].'<br />
Last Name: '.$row['LASTNAME'].'<br />
Arrival: '.$row['ARRIVAL'].</a><br />';
and you build into the page a condition that if you have the UNIQUEID coming in to look up and fill out the form such as
Company: <input type = "text" name="COMPANY" id="COMPANY" value="<?PHP echo $row['COMPANY']; ?>" />
edit to suit your database of course
sample code formatting
if(something){
do something
}else{
while(){
} # end while
} # easy to see this matches the if/else set of brackets
additional information for prepopulating
easiest would probably be on the same page. look for the userid coming into the page
if(!isset($_GET['userid'])){
# meaning we do not have a userid from a url that we generated above
# insert all your code to generate the links
}else{
# meaning we *have* a userid from a url that we generated above
# do your database query based on the userid such as
$sql='select * from mytable where userid=?';
$result=sqlsrv_query($db,$sql,array($_GET['userid']),array('Scrollable'=>'static'));
# or use mysqli, pdo or whatever... just watch for sql injection
$row=sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC));
# assuming we will not have more than one element returned
?>
First Name: <input type = "text" name="FIRSTNAME" id="FIRSTNAME" value="<?PHP echo $row['firstName']; ?>"/>
and so forth... that should get you started
So basically what you need is in the first page a lis of identical names, or ideally one name, like:
content, content, content,
FirstName, LastName
FirstName, LastName
And than if you click on either of them, than the user is redirected to another page, containing all the details of the selected person, in a form. If I am getting you right.
I would not use checkbos on the first page, simply make the person's name a hyperlink like:
http://domain.com/userdetails?userid=666
So you pass on the user id to the second page. The php code on the second page can thank draw user details from the database and populate the form.
For that purpose you can either generate the form in an echo line-by-line manner, or use DomDocument tofind and fill the relevant value fields.
Second page form generation:
you select the row with the id, than fetch it into an array, named $array.
$array = mysql_fetch_array($query); //this query selected the row for the user with the id
echo '<form method="POST">'."\n";
echo '<input name="firstname" type="text" value="'.$array['firstname'].'">'."\n";
echo '<input name="lastname" type="text" value="'.$array['lastname'].'">'."\n";
echo '</form>';
I hope you get the idea now.
I have multiple inputs (never being the same, because it's a dynamic add/remove jquery script, and it is all dependent on how many "tracks" the user wants to enter). I WILL NEVER KNOW HOW MANY WILL BE THERE. Below is an example of if a user chooses to add four "tracks" to their "mixtape".
<input size="32" class="mixtapetrack" type="textbox" id="track[1]" name="track[]" >
<input size="32" class="mixtapetrack" type="textbox" id="track[2]" name="track[]" >
<input size="32" class="mixtapetrack" type="textbox" id="track[3]" name="track[]" >
<input size="32" class="mixtapetrack" type="textbox" id="track[4]" name="track[]" >
When the form is submited (PHP POSTED), I want to take all of the inputs and combine them into one array, but sepparating them all by "^^^".
This is what I have so far, for rendering out the inputs (NOT WORKING).
$trackVAR = $_POST['track'];
$allmixtapetracks = "";
foreach ($trackVAR as $value) {
$allmixtapetracks .= '' . $value . '^';
}
And This is what i'm using to insert into my table (WORKS, tested it without the variable and entered absoloute data)
$sql = mysql_query("INSERT INTO mixtapes (title, songs, posted_by_id, description, date)
VALUES('$mixtapetitle','$allmixtapetracks','$posted_by_id','$mixtapedescription', now())")
or die (mysql_error());
In the end I want it to look like this inside of the songs row
Title of first song haha ^^^ Title of second song!!! ^^^ The
Title Of the third! ^^^ And The Title OF the Fourth
Use implode()
echo implode($trackVAR, " ^^^ ");
Replace your following code:
$trackVAR = $_POST['track'];
$allmixtapetracks = "";
foreach ($trackVAR as $value) {
$allmixtapetracks .= '' . $value . '^';
}
with this:
$allmixtapetracks=implode('^^^',$_POST['track']);
I am working on a system and i want to check if a record exist. If a record exist then it will not record the data and instead will return to the form. If the data does not exist then it will proceed to recording the data to DB.
HTML form:
<form name="studentform" onSubmit="return validate_form ( );" action="queries/insert.php" method="post">
Student Number: <input type="text" name="studentnumber"/>
College:
<select name="college" id=a></select>
Course:
<select name="course" id=b></select>
<input type="radio" name="status" value="regular" />Regular
<input type="radio" name="status" value="irregular" />Irregular
<br><br>
<hr>
<br>
Name:
<input type="text" name="lname">
<input type="text" name="fname">
<input type="text" name="mname">
Address:
<input type="text" name="address" />
<br><br>
Gender:
<select name="gender">
<option value="">---</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="submit" value="Submit">
</form>
PHP form:
$query = ("SELECT studentnumber FROM students where studentnumber = '$_POST[studentnumber]'");
$result=mysql_query($query);
if($result)
{
if(mysql_num_rows($result) >= 1)
{
echo "<script type='text/javascript'>alert('User already exist'); location.href = '../admin_home.php';</script>";
}
}
else{
$sql="INSERT INTO students (studentnumber, college, course, status, lname, fname, mname, address, gender)
VALUES
('$_POST[studentnumber]','$_POST[college]','$_POST[course]','$_POST[status]','$_POST[lname]','$_POST[fname]','$_POST[mname]','$_POST[address]','$_POST[gender]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "<script type='text/javascript'>alert('Record Successfully Added'); location.href = '../admin_home.php';</script>";
}
I don't know why but i always get the undefined index error. Maybe i've done something wrong somewhere. Thanks !!
"Undefined index" is referring to your array ($_POST, probably), and it should be a notice, not an error. Can you post the exact message?
In the meantime, switch your first line for
$query = "SELECT studentnumber FROM students where studentnumber = '".mysql_real_escape_string($_POST['studentnumber'])."'";
Also, it's helpful for debugging to print out the query to make sure it looks like you'd expect:
print $query."<br />"; // obviously
[edit]As you've now posted the error message, it becomes far more simple - $_POST['studentnumber'] does not exist. Check your form.
A good way to debug posted results is to use the code
print '<pre>';
print_r($_POST);
print '</pre>';
The problem is in your queries:
$query = ("SELECT studentnumber FROM students where studentnumber = '$_POST[studentnumber]'");
$_POST[studentnumber] is not correct. It needs to be $_POST['studentnumber']. Notice the quotes around the key.
I suggest doing it this way:
$query = sprintf("SELECT studentnumber FROM students where studentnumber = '%s'"
, mysql_real_escape_string($_POST['studentnumber']));
Change all your queries accordingly.
try with this:
if( isset($_POST['submit']) ){
$student_num = mysql_real_escape_string( $_POST['studentnumber'] );
// Set all the require form fields here with mysql_real_escape_string() fun
if( !empty($student_num) ){
// Your Query Here
}
else{
echo 'Value not Set in Student Number Field!';
}
}
Edit: first check all the fields after isset($_POST['submit']) so that you confirm about all the values are properly getting or not
after getting all the required values start your query
Hello i have a html form in side php everything is working fine and i have just been showed how to do hidden fields from this website.
I have a submit button on each and every result letting the user pick what they want. When they press the submit button i want the info to be submitted and added to the database. But for some reason when the user click submit on item 1 it adds the last item into the database e.g item 6 ?? So there is 6 results and a submit button for each one so 6 buttons. When the user presses submit on number 1 item it submits number 6 for some resson.
<form method="post" action="buydo.php">
<label><br />
<br />
</label>
<p>
<?php
$sql = "SELECT * FROM sell
ORDER BY Pokemon_level ASC";
$res = mysql_query($sql) or die(mysql_error());
while ($v = mysql_fetch_array($res)) {
echo '
<div class="auction_box">
<img src="http://myrpg.net/new_rpg/'.$v['Pokemon_pic'].'" width="100" height="100"><br/>
£'.$v['price'].'<br/>
<label id="pokemonName'.$v['id'].'">'.$v['pokemon_name'].'</label><br/>
<label>Level '.$v['Pokemon_level'].'</level><br/>
<label>Exp '.$v['exp'].'</level><br/>
<label>Time Left:';
echo '</label>
<br/>
<input type="hidden" name="Name" value="'.$v['pokemon_name'].'">
<input type="hidden" name="level" value="'.$v['Pokemon_level'].'">
<input type="hidden" name="vid" value="'.$v['id'].'">
<input type="hidden" name="price" value="'.$v['price'].'">
<input type="hidden" name="exp" value="'.$v['exp'].'">
<input type="submit" id="'.$v['id'].'" class="buy_submit" value="Buy Now" /> </div>';
}
?>
</p>
<p> </p>
</form>
That is the select with the submit buttons for each result.
Then i insert the info they have chosen.
include 'config.php';
session_start() ;
$name = mysql_real_escape_string($_POST['Name']);
$Pokemon_level = mysql_real_escape_string($_POST['level']);
$idofpokemonsell = mysql_real_escape_string($_POST['vid']);
$price = mysql_real_escape_string($_POST['price']);
$exp = mysql_real_escape_string($_POST['exp']);
$sql = "SELECT * FROM `users` WHERE `username` = '" . $_SESSION['username'] . "'";
$result = mysql_query($sql) or die(mysql_error());
$values = mysql_fetch_array($result);
echo $values['money'] ;
if ( $values['money'] == $price ) {
echo "Give them the pokemon yay";
}
if ( $values['money'] > $price ) {
mysql_query("INSERT INTO `user_pokemon` (`pokemon`, `belongsto`, `exp`, `slot`, `level`) VALUES ('$name','" . $_SESSION['username'] . "','$exp','0','$Pokemon_level')") or die(mysql_error());
echo "Your money is over";
}
Like i say the insert only inserts item 6 no matter if u press item 1 - 5 it inserts 6 the level , exp, name everything of item 6
Since you display it all in one form, the latest values are overwriting the earlier ones. Try making each item its own form by moving your <form> and </form> tags inside your while loop
or try to track with jquery clicked button id and fill a hidden field with the clicked id
This is because your html is wrong. The values get overwritten
Basically you have to options:
render a form for each item
do it with a dropdown or radio buttons
With the second option you need to rewrite your code more, but it is more elegant, if you ask me. So what do you need to do.
Move the submit button out of the loop
Only generate code with the id and labels like this:
<option value="'.$v['id'].'"/>YOUR LABEL</option>
When the form is submitted, you have to retrieve the values corresponding to your the submitted id