I want to create a page, where I can create Keys, and save them in a databank, but I got some problems.
There is a textfield, where you can write in, how many keys you want to create. For example I write 5 in, but it does only 3 entrys in the databank, but down in the code is an echo command, which says that he did it 5 times, here is a screenshot:
Also, what I noticed is, that the website is doing max 3 entrys in the databank, for example I write 10, and he still only does 3 entrys.
If I write only 1-3 keys, the while-loop is working.
echo "<p><a href='key.php'>Betakey administration</a></p>";
echo "<form action='#' method='POST'>";
echo "<input type='number' name='numberkeys' placeholder='Number of Keys'>";
echo "<br>";
echo "<input type='submit' name='createkey' value='Create Key'>";
echo "<input type='submit' name='keyanzeigen' value='Show all Keys'>";
echo "<input type='submit' name='unusedkeys' value='Show all unused Keys'>";
echo "<input type='submit' name='usedkeys' value='Show all used Keys'>";
echo "</form>";
$createdkeys=0;
if(isset($_POST['createkey'])){
$minus= array(5,10,15);
while($createdkeys<$_POST['numberkeys']){
//creation of the keys
for($i=1;$i<20 ;$i++){
if(in_array($i,$minus)){
$key=$key."-";
}
else{
$key=$key.mt_rand(0,9);
}
//the key looks like this: 1234-5678-1234-5678
}
//here it checks, if the key already exists
$check = mysqli_query($db,"SELECT keyy FROM keyys WHERE keyy='$key'");
$check = mysqli_fetch_array($check);
//if the code not exists, the website will insert the key in the databank
//the website will echo the variable $createdkeys and increase the variable $createdkeys by one
if(!isset($check)){
$createdkeys= $createdkeys+1;
echo "<p>".$createdkeys."</p>";
mysqli_query($db,"INSERT INTO keyys VALUES('$key','NO')");
}
}
}
Related
I'm trying to create a very easy stock managing system. I'm able to show all the items in my table 'parts' and i'm showing the amount in a textbox. However, when i change the value from, for example, 0 to 5 in the textbox and i press my submit button, it doesn't update the stock.
Below is my code, i don't have alot of experience with update querys but i've read about it on php.net, obviously.
<?php
echo "<table width=\"800\" class=\"nieuws\">";
$db=mysqli_connect("localhost","root","","lichtwinkel");
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<form method='post' action=''>";
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal' value=$row[voorraad] /></td>";
echo "<td><input type='submit' id='update' name='update' value='Update' /></td>";
echo "</tr>";
}
echo "</table>";
if(isset($_POST['aantal']) && $_POST['update']) {
$y = $_POST['aantal'];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '$y' WHERE partnr = $row[0]");
}
echo "</form>"
?>
Simply said, what i'm trying to achieve is the following:
Whenever i change the value displayed in the texbox, and i press my submit button, i want it to update the value in the database.
Does anyone know what i'm doing wrong? Any ideas? Articles i should read?
All help would be appreciated.
Thank you.
As i see, you were doing it wrong at all.
First you can't use form tag within more then one td element.
You were didn't close the form tag, only at end. (So if it loops 6 times, you will have 6 forms open, but one ended!).
At update, you're selecting row[0] - it's outside of loop with rows?
Even if you update it, it will show wrong results again. Update should be above selects! So it picks up newly updated value.
What to do:
First make one form for all updates.
Use your submit button to have value DATABASE_ID.
Make the name of "aantal" to "aantalDATABASE_ID".
At submit check for $_POST['update'], and use it's value (DATABASE_ID) to get input $_POST["aantal".$_POST['update']].
Do update, you have all you need.
Example:
<?php
echo "<form method='post' action=''>";
echo "<table width=\"800\" class=\"nieuws\">"
$db=mysqli_connect("localhost","root","","lichtwinkel");
if(isset($_POST['update']) && !empty($_POST['update'])) {
$y = $_POST['aantal'.$_POST['update']];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '".$y."' WHERE partnr = '".$_POST['update']."'");
}
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal".$row[0]."' value='".$row[voorraad]."' /></td>";
echo "<td><input type='submit' id='update' name='update' value='".$row[0]."' /></td>";
echo "</tr>";
}
echo "</table>";
echo '</form>';
?>
After all, take care about SQL Injections. "aantal" value is user input. As the submit value can be changed.
I am on the process of modifying old code. I want to have set of input fields as rows in a table with Update button along with them. Below code is included in seperate ajax_edit_designation.php page which is being called by the edit_designation page. When the update is selected it should enter the new value to the database. The script required for it is written in the edit_designation.php page.
I would like to know whether what I am doing is correct or wrong. Because for some reason it doesn't carryout required action when the 'Update' button is selected. In the browser console it doesn't throw any exceptions either. What am I doing wrong?
Is it something that happen due to some kind of version issue? Or am I really doing something wrong?
echo "<table>";
while($row_tay = mysql_fetch_array($result_tay)){
$temp++;
$designation = $row_tay['designation'];
$designationID = $row_tay['designationID'];
echo "<tr ";
if($temp%2 == 1)
{
echo " bgcolor =\"#F3F3F3\" ";
}
else
{
echo " bgcolor =\"#DBDBDB\" ";
}
echo " >";
echo "<form id=\"myform".$designationID."\" method=\"post\">";
echo "<td class=\"bodytext\">";
echo "<input type=\"text\" id=\"designation\" size=\"50\" value=\"".htmlentities($designation)."\" name=\"designation\" />";
echo "</td>";
echo "<td width=\"120\" align=\"center\">";
echo "<input name=\"addcat\" id=\"addcat\" type=\"submit\" value=\"Update\" class=\"Submit_Button_long\">";
echo "<input type=\"hidden\" name=\"designationID\" value=\"".$designationID."\">";
echo "</td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
Because, same thing happened to me in another page also, where I added seperate form tags as above. In that case, previously it had given all the Update buttons under same tag. When I remove that earlier form tag and add only the new tags as above, they didn't work as now. But if I add that previously available tag, by wrapping all those forms created with the table, Update buttons worked. What could be the reason. Please help?
I'm facing such a problem,
I have a database bank with lots of questions and their joined answers 'multiple choice system'!
I wrote a PHP script to retrieve (15) questions each time a user enters the page, 'index.php' as a radio-buttons.
Now my bass asked me to change the method of showing the questions! so, instead of 15 questions at the same page! he asked to show one question per time, something like this:
================================================================================
1. Question TEXT:
a. 1st variant
b. 2nd variant
c. 3rd variant
d. 4th variant
<NEXT QUESTION>
================================================================================
so by clicking on the button which is a "submit" the script should redirect me to the second part of the file which must show the second question, and from the second as shown above to the third --> forth --> ... --> 15th question
================================================================================
1st question is: Correct //This part will check the correction/incorrection of the previous question -_-
2. Question2 TEXT:
a. 1st variant
b. 2nd variant
c. 3rd variant
d. 4th variant
<NEXT QUESTION>
================================================================================
bottom line, I am able to bring a 15 question on the page and after checking the answers I give the results. but I could figure out how to distribute these questions into individual pages!! as
'index.php?question=1'
'index.php?question=2'
'index.php?question=3'
...
'index.php?question=15'
Thank you guys!
so as I said, I wanted to distribute the results retrieved from one query into individual pages, and I came up with this idea,
1. Did the same query
SELECT * FROM database ORDER BY RAND() LIMIT 10
then I saved the IDz of the retrieved questions into an array just like that
$id = array();
$_SESSION[corr] = 0;
$_SESSION[incorr] = 0;
while ($row = mysql_fetch_assoc($query) {
$id[] = $row[ID];
}
Then I saved the array into a SESSION to pass it in every required page as follows
$_SESSION[ID] = $id;
after that I set a link such as
echo "<a href=test.php?question=1>Click To Start</a>";
and last but not least did something like this
if ($_REQUEST['question'] == '1') {
$id = $_SESSION['id'];
$sql = mysql_query("SELECT * FROM `test` WHERE `id` = '$id[0]'") or die("Error" .mysql_error());
$row = mysql_fetch_assoc($sql);
echo "<font color=green>Correct: " .$_SESSION[corr]. "</font></br>";
echo "<font color=red>Incorrect: " .$_SESSION[incorr]. "</font></br>";
echo "$row[v]<br>";
echo "<form action='test.php?question=2' method='POST'>";
echo "<input type=radio name=answer1 value=1>$row[o1]</input><br>";
echo "<input type=radio name=answer1 value=2>$row[o2]</input><br>";
echo "<input type=radio name=answer1 value=3>$row[o3]</input><br>";
echo "<input type=radio name=answer1 value=4>$row[o4]</input><br>";
echo "<input type=submit name=submit value=Check>";
echo "</form>";
$_SESSION['previous_id'] = $row['id'];
exit;
and in the next part "test.php?question=2"
I firstly checked if the previous question was answered correctly as the following
if ($_REQUEST['question'] == '2') {
$id = $_SESSION['id'];
$answer = $_POST['answer1'];
$grade = mysql_query("SELECT * FROM `test` WHERE `id` = {$_SESSION['previous_id']}") or die();
$right = mysql_fetch_assoc($grade);
if ($answer == $right[g]) {
$_SESSION['corr']++;
echo "<font color=green>Correct</font></br>";
}
else {
$_SESSION['incorr']++;
echo "<font color=red>Incorrect, check lesson #".$right[lvl]."</font></br>";
}
$sql = mysql_query("SELECT * FROM `test` WHERE `id` = '$id[1]'") or die("Error" .mysql_error());
$row = mysql_fetch_assoc($sql);
echo "$row[v]<br>";
echo "<form action='test.php?question=3' method='POST'>";
echo "<input type=radio name=answer2 value=1>$row[o1]</input><br>";
echo "<input type=radio name=answer2 value=2>$row[o2]</input><br>";
echo "<input type=radio name=answer2 value=3>$row[o3]</input><br>";
echo "<input type=radio name=answer2 value=4>$row[o4]</input><br>";
echo "<input type=submit name=submit value=Check>";
echo "</form>";
$_SESSION['previous_id'] = $row['id'];
exit;
}
That worked just 100% as I wanted, but it is just not feeling professional, because I had to rewrite that code, or copy and edit the same above shown codes lots of times, considering I am trying to post 10 or 15 questions separately..
hopefully that helps some one else!
I print this form in a web page with php:
<?php
include("connectDB.php");
$mySQL=new MySQL();
$queryResult=$mySQL->query("SELECT nombre, precio, id_producto FROM productos");
echo "<form action= 'checkout.php'> method=''POST'";
while($datos=$mySQL->fetch_array($queryResult))
{
$n = 1;
$nombre=$datos['nombre'];
$id_producto=$datos['id_producto'];
$precio=$datos['precio'];
echo "<h1>$nombre</h1>";
echo "<input type=\"checkbox\" name=\"$id_producto\" value=\"$nombre\"> Cantidad: <input type=\"number\" name=\"points\" min=\"1\" max=\"20\" step=\"1\" value=\"1\"><br>";
echo "<h3> Precio: $precio<br>";
}
echo "<br>";
echo "<input type=\"submit\" class=\"button\" value=\"Comprar\">";
echo "</form>";
?>
So it displays a list (which is a form within) of items which can be selected or checked, on submit, I want to do the $_POST[''] of only the checked items, how could I solve this?
When such checkboxes are printed, only those values which were checked are submitted.
If i understood you correctly you wanted to retrieve those that got posted, which you can follow with this simple method
foreach($_POST as $post_key => $post_value){
//Check that the particular input is int and numeric, since i believe the name is the id
if(is_numeric($post_key) && is_int($post_key){
//Here goes your code, $post_key is the id and $post_value is the $nombre
//Although i admit that i have no idea what nombre is since it is in another language. Forgive me if id_producto is not numeric and unique.
}
}
<?php
$i=1;
$ph='php';
echo "<form method=\"post\" action=\"\">";
$query=mysql_query("select question,id from ques where category=\"$ph\" order by rand() limit 5",$con);
if(!$query)
{
echo mysql_error().'query failed';
}
$ans=1;
while($value1=mysql_fetch_array($query))
{
echo "Q-$i"." ";
echo $value1['question']."<br />";
$i++;
$qno=$value1['id'];
$query1=mysql_query("select id,option1,option2,option3,option4 from ques where id=$qno");
if(!$query1)
{
echo mysql_error().'query failed';
}
while($value2=mysql_fetch_array($query1))
{
$opt=$value2['option1'];
$opt1=$value2['option2'];
$opt2=$value2['option3'];
$opt3=$value2['option4'];
$id=$value2['id'];
echo "<input type=\"radio\" value=\"$opt\" name=\"$ans\">";
echo "<span class=\"margin\">$opt</h1>";
echo "<input type=\"radio\" value=\"$opt1\" name=\"$ans\">";
echo "<span class=\"margin1\">$opt1</h2>";
echo "<input type=\"radio\" value=\"$opt2\" name=\"$ans\">";
echo "<span class=\"margin2\">$opt2</h3>";
echo "<input type=\"radio\" value=\"$opt3\" name=\"$ans\">";
echo "<span class=\"margin3\">$opt3</h4>"."<br /><br />";
$ans++;
}
}
echo"<center>"."<input type=\"submit\" name=\"submit\" value=\"submit\">"."</center>";
echo "</form>";
if(isset($_POST['submit']))
{
$correct=$_POST[1];
$_SESSION['value']=$correct;
/*$correct1=$_SESSION['value'];
echo $correct1;*/
$correct2=$_POST[2];
$_SESSION['value1']=$correct2;
/*$correct3=$_SESSION['value1'];
echo $correct3;*/
$correct4=$_POST[3];
$_SESSION['value2']=$correct4;
/*$correct5=$_SESSION['value2'];
echo $correct5;*/
$correct6=$_POST[4];
$_SESSION['value3']=$correct6;
/*$correct7=$_SESSION['value3'];
echo $correct7;*/
$correct8=$_POST[5];
$_SESSION['value4']=$correct8;
/*$correct9=$_SESSION['value4'];
echo $correct9;
$j=1;
while($j<=$i-1)
{
$correct=$_POST[$j];
$_SESSION['$j']=$correct;
$j++;
$correct1=$_SESSION['$j'];
echo $correct1."<br />";
}*/
header("location:quizresult.php");
}
?>
I don't know how to send id of selected option to the next page.I want to retrieve the id of selected option in the next page because from this i can get the correct answer of the selected question or any other solution of this and i get the only one id on the next page
There are several ways how to carry over values to the next page. I used the following successfully before:
1. Use a $_SESSION variable
Save the option values in session variables and retrieve them on the next page.
2. Use hidden form fields
You can save them as part of the form but in a hidden form field and retrieve it on the next page via $_POST.
An excellent tutorial (4 pages) for HTML forms and its various inputs is here:
http://www.javascript-coder.com/html-form/html-form-tutorial-p1.phtml
3. Use a http query
You can append a http query to your link to the next page, containing the values, i.e.
http://www.example.com/next-page?option1=value1&option2=value2
On the next page you can use parse_url($url, PHP_URL_QUERY) to extract the values. I would recommend that only if you do not submit a form though. I use this option if I create action buttons that are linking to the next (or the same) page and a single action has to be performed before the page is displayed.
Some generic info about query strings: http://en.wikipedia.org/wiki/Query_string
$qno= $_SESSION['id'] = $value1['id'];
Id is being saved in $_SESSION['id'] and will be available on the ext page also.
retrieve it like this
$id = $_SESSION['id'];