I have a page that displays the data of a mysql entry, depending on the link the user clicked ($pagename).. Im wondering how I can create a very basic rating system, that will consist of a form with drop down options of 1 to 5, and when the user submits this value, it posts the data to the corresponding ID of the entry thats currently on the page.
<?php
$pagename = $_GET['name'];
$sql = "SELECT * FROM tblCocktail WHERE name = '$pagename' LIMIT 1";
/*$sql = sprintf(%sql, mysql_real_escape_string($pagename));*/
$result = mysql_query($sql);
if(!$result) {
// error occured
}
$data = mysql_fetch_assoc($result);
echo "<p class=\"paratitle\">".$data["name"]." </p>";
echo "<p class=\"paratitle3\">".$data["howto"]." </p>";
echo "<p class=\"paratitle2\">".$data["ingredient1"]." </p>";
echo "<p class=\"paratitle3\">".$data["quantity1"]." </p>";
echo "<p class=\"paratitle2\">".$data["ingredient2"]." </p>";
echo "<p class=\"paratitle3\">".$data["quantity2"]." </p>";
echo "<p class=\"paratitle2\">".$data["ingredient3"]." </p>";
echo "<p class=\"paratitle3\">".$data["quantity3"]." </p>";
echo "<p class=\"dateadded\">".$data["dateadded"]." </p>";
?>
</div>
</div>
<div id="cont2">
<div id="contentwrap">
<form method="POST" action="addrating.php" >
<input type="hidden" name="cocktailID" value="<?=$data["id"]?>">
<select id="ratinglevel" name="ratinglevel">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<input type="submit" value="submit" />
</form>
addrating.php:
<?php
mysql_select_db("mwheywood", $con);
//insert cocktail details
$sql="INSERT INTO tblRating (cocktailID, value, counter)
VALUES
('$_POST[id]','$_POST[ratinglevel]','1'";
if (!mysql_query($sql,$con))
{
die('Error: you fail at life' . mysql_error());
}
echo "<p>Thanks for voting</p>"
?>
the table I want to save the rating into is linked via "cocktailID" to the data that is being echo'd in the above code.
and the table structure of "tblRating" is: ratingID, cocktailID, value, counter..
I therefore want the option value to save to the corresponding "cocktailID", in the "value" field, and a "1" posted to the counter field.
-any help is appreciated -matt
Just include the current ID when you send teh form like this
<form method="POST" action="addrating.php" >
<input type="hidden" name="cocktailID" value="<?=$data["id"]?>">
<select id="ratinglevel" name="ratinglevel">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</form>
This will then return the ID along with the form results at which time you will have the cocktailID available
if (isset($_POST['cocktailID']) && isset($_POST['ratinglevel']))
$sql = "INSERT INTO tblRating SET cocktailID = ".mysql_real_escape_string($_POST['cocktailID']).", value = ".mysql_real_escape_string($_POST['ratinglevel']).",counter = 1";
$result = mysql_query($sql);
if(!$result) {
// error occured
}
}
Related
I have a list which is displayed from my database:
<<b>List</b>
<?php
$select = "SELECT * FROM quabits.inregistrare";
$result2 = mysql_query($select);
?>
<form id="raport" name="save">
<table cellspacing='0' cellpadding='0'>
<tr><td>Selectează: </td>
<td><select name="den" id="den" onchange="rulare(this.value)">
<option value="">Selectați un profesor</option>
<?php
while($row=mysql_fetch_array($result2)){
echo "<option value='" . $row['Email'] ."'> " .$row['Nume'] ." ". $row['Prenume'] ."</option>";
}?>
</select>
</td></tr>
</table>
</form>
<input id="save" name="save" type="submit" value="Submit" action="POST"/>
After the drop down menu I put a button to submit.
And after a value is selected and click for submit, I want to save into my database (quabits table inregistrare) the value.
Here I'm lost. Any help ?
If you have a select like the following:
<select name="den" id="den" onchange="rulare(this.value)">
<option value="">Selectați un profesor</option>
.....
</select>
You would retrieve the value of the select using $_REQUEST['den'].
Make sure you add the method type to your form, for example
<form id="raport" name="save" method="POST">
in PHP you would store the POST value:
$den = $_POST['den'];
$den = $_POST['den'];
$link = mysqli_connect("host","user","password","database",3306
$sql = "Insert into table ('fieldname') VALUES ('$den');
mysqli_query($link,$sql);
Very basic solution, but i the question is not very understandable....
I can't seem to get the following code to make a dropdown menu that contains data from a mysql database. The "include('connect.php');" connects to the mysql database and I know it works on separate pages. Any suggestions?
Below is the entire code.
listCustomer
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($r = mysql_fetch_array($result))
{
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
}
echo "</select>";
?>
<BR>
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
<?php
include('connect.php');
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
From the looks of things, you're missing an opening option tag, so it's just outputting "Dropdown" as a line of text.
Edit
Just to be completely transparent, because I did not have connect.php, I had to add my own DB connections. My whole page looked thusly:
<?
//Adding to display errors.
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>Find Customer's Albums Page</H1>
From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
<HR>
<FORM ACTION="listCustomer.php" METHOD="POST"/>
Customer:
<select name="mydropdownCust">
<option value="101">101</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="104">104</option>
<option value="105">105</option>
<option value="106">106</option>
<option value="107">107</option>
<option value="108">108</option>
<option value="109">109</option>
<option value="110">110</option>
</select>
<BR />
<?php
// BEGIN ADDED CONNECTION HACKY GARBAGE
$con=mysql_connect("localhost","root","root");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$selected = mysql_select_db("sample",$con)
or die("Could not select examples");
// END ADDED CONNECTION HACKY GARBAGE
$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['Cnum'].">".$r['CName']."</option>";
}
echo "</select>";
?>
<BR />
<INPUT TYPE="SUBMIT" Value="Submit"/>
</FORM>
<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>
First off, you are missing an option opening tag, as correctly mentioned by stslavik. But this is not causing the issue here as it seems (it's auto-corrected by the browser - in my tests atleast).
Secondly, this wont work (problem causer):
echo "<option value=$r["Cnum"]>$r["CName"]</option>";
You should use
echo "<option value=".$r["Cnum"].">".$r["CName"]."</option>";
or, as I always prefer single quotes to enclose echo or print output strings:
echo '<option value='.$r['Cnum'].'>'.$r['CName'].'</option>';
Third alternative (complex syntax: What does ${ } mean in PHP syntax?)
echo "<option value={$r["Cnum"]}>{$r["CName"]}</option>";
assuming you get data from the database try this
echo "<option value={$r['Cnum']}>{$r['CName']}</option>";
try,
echo "<option value=' . $r['Cnum'] . '>' . $r['CName'] . '</option>";
instead of
echo "<option value=$r[Cnum]>$r[CName]</option>";
can someone please show me how i would post select drop down values along side my text input values into mysql database.
I have the following and am not sure how dropdown values should be handled.
Thanks
<form action=\"includes/welcomestats.php\" method=\"post\" id=\"form1\">
<input type=\"text\" name=\"location\" id=\"location\" maxlength=\"30\" placeholder=\"Location: e.g. London\">
Enter Your Location</label><br/>
<br/>
<input type=\"text\" name=\"local_station\" id=\"local_station\" maxlength=\"30\" placeholder=\"Local Train Station\"><label> Enter a Local Train Station</label><br/><br/><br/>
<h5>About You</h5><br/>
<select name=\"formGender\" style=\"min-width:125px;\">
<option value=\"\">Select...</option>
<option value=\"M\">Male</option>
<option value=\"T\">Female</option>
</select>
<label> Enter Your Gender</label>
<br/><br/>
<input type=\"submit\" class=\"welcome-submit2\" name=\"submit\" value=\"Next ->\" id=\"submit\"/>
</form>
<?php
require_once("session.php");
require_once("functions.php");
require('_config/connection.php');
?>
<?php
session_start();
include '_config/connection.php';
$location = $_POST['location'];
$result = mysql_query("SELECT location FROM ptb_profiles WHERE id=".$_SESSION['user_id']."");
if(!$result)
{
echo "The username you entered does not exist";
}
else
if($location!= mysql_result($result, 0))
{
echo "";
$sql=mysql_query("UPDATE ptb_profiles SET location ='".addslashes($display_name)."' WHERE id=".$_SESSION['user_id']."");
}
Select field values are handled same way as any other field. So if you have select field like -
<select name=\"formGender\" style=\"min-width:125px;\">
<option value=\"\">Select...</option>
<option value=\"M\">Male</option>
<option value=\"T\">Female</option>
</select>
You can use
$gender = $_POST['formGender']
Update
That will give you either M or F.
With this way, I get results from database and "print" them. But I don't know how I will update those results when I press the submit button!!! I just need an idea or something for the next step. Thank you in advance!!!
Here is an example of my code...
<?php // DATABASE QUERY
$query="SELECT countdown_module, hometeam_position
FROM jos_gm_nextmatch
WHERE id = 1";
$result=mysql_query($query);
// DATABASE VARIABLES
$countdown_module = mysql_result($result,$i,"countdown_module");
$hometeam_position = mysql_result($result,$i,"hometeam_position"); ?>
<form action="***.php" method="post" name="form">
<input name="countdown_module" value="<?php echo $countdown_module ?>" type="text" />
<select name="hometeam_position">
<option value="<?php echo $hometeam_position ?>"><?php echo $hometeam_position ?></option>
<option disabled="disabled" value="...">...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">3</option>
<option value="5">5</option>
<input name="submit" type="submit" value="UPDATE" />
</form>
You would use the form action to redirect to a script where you do the update. On this script you can access the the forms input elements by using the $_POST array. As for how to do update queries, an example could be:
$query="UPDATE mytable
SET title = '".$title."', name = '".$name."', date = '".$date."'
WHERE id = ".$id;
$result=mysql_query($query);
UPDATE:
An example of the script could be:
$hometeam_position = $_POST['hometeam_position']; //access the selected option when submitting
$countdown_module = $_POST['countdown_module']; //access the text input
$query = "UPDATE jos_gm_nextmatch SET countdown_module = '".$countdown_module."', hometeam_position = '".$hometeam_position."' WHERE id = 1";
$result=mysql_query($query);
You could before or after selecting the fields from the database simply increment them
...
if (isset($_POST['submit'])) {
$stmt = "UPDATE jos_gm_nextmatch
SET countdown_module = " . $_POST['countdown_module'] .
" , hometeam_position =" . $_POST['hometeam_position'] .
" WHERE id=1";
mysql_query($stmt);
}
mysql_close();
My current form submits data to two different tables, and I want the auto_incremented value from one table to also be stored in the second table.
<form method="POST" action="addcocktail.php" >
Cocktail Name: <input type="text" name="cocktailname" />
How To: <input type="text" name="howto" />
<br>
<select id="selectingred1" name="selectingred1">
<?php
$sql = "SELECT ingredientID, name FROM tblIngredient ".
"ORDER BY name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo "<option value=\"".$row['ingredientID']."\">".$row['name']."</option>\n ";
}
?>
</select>
<select id="quantity1" name="quantity1">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<br>
<input type="submit" value="add" />
</form>
addcocktail.php:
<?php include("databasecon.php"); ?>
<?php
mysql_select_db("mwheywood", $con);
//insert cocktail details
$sql="INSERT INTO tblCocktail (name, howto)
VALUES
('$_POST[cocktailname]','$_POST[howto]')";
$sql2="INSERT INTO tblRecipe (ingredientID, quantity)
VALUES
('$_POST[selectingred1]','$_POST[quantity1]'),
('$_POST[selectingred2]','$_POST[quantity2]'),
('$_POST[selectingred3]','$_POST[quantity3]'),
('$_POST[selectingred4]','$_POST[quantity4]')";
if (!mysql_query($sql,$con))
{
die('Error: you fail at life' . mysql_error());
}
echo "cocktail added";
if (!mysql_query($sql2,$con))
{
die('Error: you fail at life' . mysql_error());
}
echo "ingredients added";
mysql_close($con);
?>
so to put it simply, when I submit my form. I want the "cocktailID" of the posted data to "tblCocktail" to also save into "tblRecipe"
after executing the insert query, you can get the insert id if succeeded with mysql_insert_id() function.