I have a form for adding products and their detail to a mysql db.
I have another form that shows the products where I can remove the product from the db, I can also hide or show the product in the website.
I need to be able to edite the product details too. Is there any way that when I choose to edit a products I can get it details to appear in the first form again? The form is for adding details so can it be used to edit them too?
This is my code for first product adding form.
<form class='productsaddform' action='productsadd.php' method='post'
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<?php
include '../inc/categorydropdown.php';?>
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file"
id="image_upload_box" /></p>
<b>Name</b><br /><input type=text name="aname" /><br />
<b>Brand</b><br /><input type=text name="abrand" /><br />
<b>Code</b><br /><input type=text name="acode" /><br />
<b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea>
<br />
<b>Product Spec</b><br /><textarea rows="12" cols="40" name="aspec"></textarea><br />
<b>Price</b><br /><input type=text name="aprice" /><br />
<p><label for="cat">Category</label>
<select name="cat" id="cat">
<?php echo $op;?>
</select><br />
<label for="subcat">Subcategory</label>
<select name="subcat" id="subcat"> </select></p>
<br />
<br />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/add_products.php'; ?>
</form>
And this is the form to display the products
<form class='productsremoveform' action='productsadd.php' method='post'>
<?php include '../inc/categorydropdown.php'; ?>
<?php include '../inc/remove_products.php'; ?>
<span class='formheading'>Remove/Hide/Show Products</span><br /><br />
<p><label for="cat">Category</label>
<select name="cat" id="removecat"> <?php echo $op;?> </select><br />
<label for="subcat">Subcategory</label>
<select name="subcat" id="removesubcat"> </select>
<input type='submit' name='show' value='Show' /> </p>
<?php
include '../inc/connect.php';
if(isset($_POST['show'])){
$subcat = intval($_POST['subcat']);
$q = "SELECT * FROM products WHERE subcat = $subcat";
$result = $link->query($q);
if($result){
while($row=mysqli_fetch_array($result)){
echo "<div class='removeproducts'>";
echo "<input type='checkbox' name='remove[{$row['id']}]' value='Remove'>Remove";
echo "<br />";
echo "<input type='checkbox' name='edit[{$row['id']}]' value='Edit'>Edit";
echo "<br />";
if ($row['status'] == 1){
echo"<input type='checkbox' name='hide[{$row['id']}]' value='Hide'>Hide";
echo "<br />";
}
if ($row['status'] == 2){
echo"<input type='checkbox' name='show[{$row['id']}]' value='Show'>Show";
echo "<br />";
}
echo "<br />",
"<span class='productthumbcode'>",
"{$row['code']}",
"</span>",
"<div id='thumb'>",
"<img class='resizedimage' src='{$row['image']}' alt='{$row['name']}' />",
"</div>",
"</div>";
}
echo "<br style='clear:both' />";
}
else{
echo mysqli_error();
}
}
?>
<input type='submit' id='submit' name='submit' value='Submit' />
</form>
EDIT;
This is the form on my new edit page.How do i get the relevent details from the db into the input values?
<?php if (isset($_GET['pid'])) { ?>
<form class='productsaddform' action='edit_products.php' method='post'
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<span class='formheading'>Edit Product</span><br /><br />
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file"
id="image_upload_box" /></p>
<b>Name</b><br /><input type="text" name="aname" />
<br />
<b>Brand</b><br /><input type=text name="abrand" /><br />
<b>Code</b><br /><input type=text name="acode" /><br />
<b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea>
<br />
<b>Product Spec</b><br /><textarea rows="12" cols="40" name="aspec"></textarea><br />
b>Price</b><br /><input type=text name="aprice" /><br />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/edit_products.php'; ?>
</form>
<?php } ?>
This is what i have now, but the form doesnt display now
<?php if (isset($_GET['pid'])) {
$q = mysqli_query($link, "SELECT * FROM products WHERE id = '".$_GET['pid']."'") or
die (mysqli_error());
$row = mysqli_fetch_array($q); ?>
<form class='productsaddform' action='edit_products.php' method='post'
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<span class='formheading'>Edit Product</span><br /><br />
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file"
id="image_upload_box" /></p>
<b>Name</b><br /><input type="text" name="aname" value=<?php"$row['name'];"?> />
<br />
<b>Brand</b><br /><input type=text name="abrand" /><br />
<b>Code</b><br /><input type=text name="acode" /><br />
<b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea>
<br />
<b>Product Spec</b><br /><textarea rows="12" cols="40" name="aspec"></textarea><br />
<b>Price</b><br /><input type=text name="aprice" /><br />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/edit_products.php'; ?>
</form>
<?php } ?>
EDIT:
Now I want to be able to search products by code in my first form then have that products details displayed in the form in my edit_product page as it is when i edit a product as we sorted. Any idea how to go about this?
This wouldn't be the necessarily the cleanest way to do it, but you could get the information from the db (as you would normally), put it into an array and for each input have something like this:
<input type="text" name="aname" value="<?php echo (isset($array['name'])) ? $array['name'] : ''; ?>" />
Basically, it's an inline if statement that checks to see if the variable is set and if it is, it sets the value of the input to that.
Hope this helps!
If you have any questions on this, let me know :)
EDIT
To answer your question from the comments you could have a link for each product which then takes you to an edit product page e.g.
Link:
AND then just have a page similar to you first page that checks to see if the get variable is set.
Related
I need to update the selected row in the page. I have a webpage where user as to enter the id. In the second page the all the rows and values with the same Id entered as to be displayed for editing. How can i do this..
Here is the code
<form method="post" action="edituser.php">
<label type="text" name="name" maxlength="50" size="30" class="label">Enter the Membership Number</label><br />
<input type="text" name='id' placeholder="enter Membership Number" class="input" size="40"/><br />
<span class="field">(* Required field)</span><br /><br />
<input type="submit" name="submit" value="SUBMIT" class="button"><br /><br /><br /><br />
</form>
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("anthonys");
if(isset($_POST['submit']))
{
$id= $_POST['id'];
if( ! ctype_alnum($id) )
die('invalid id');
$query = "SELECT id FROM `member` WHERE `id` =$id";
$run = mysql_query($query);
if(mysql_num_rows($run)>0){
echo "<script>window.open('edit.php?id=".$id."','_self')</script>";
}
else {
echo "<script>alert('Membership No is Invalid!')</script>";
}
}
?>
<
h2>Application for the Membership</h2><br /><br />
<table border="0px" style="border-collapse:collapse; width:810px;" align="center">
<tr>
<td>
<form name="XIForm" id="XIForm" method="POST" action="pdf/pdf2.php">
<label type="text" name="uid" maxlength="50" size="30" class="label">Membership No</label><br />
<input type="text" name="id" id="id" value="<?php if(isset($_GET['id'])) { echo $_GET['id']; } ?>" readonly> <br /><br />
<label type="text" name="fathername" maxlength="50" size="30" class="label">Father`s Name</label><br />
<input name="fathername"name="fathername" placeholder="" class="input" size="40"value="<?php if(isset($_GET['fathername'])) { echo $_GET['fathername']; } ?>"> <br /><br />
<input type="hidden" name="formType" id="formType" value="reg"/>
<input type="button" name="XISubmit" id="XISubmit" value="ADD" class="button" />
<br /><br /><br /><br />
</form></td>
</tr>
</table>
</div>
</div>
</section>
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("anthonys");
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE member SET fathername='$_POST[fathername]' WHERE id='$_POST[hidden]'";
$run=mysql_query($UpdateQuery);
};
echo "<table border=1>
<tr>
<th>Fathers_Name</th>
"</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action=edit.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=Father_name value=" . $record['fathername'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['id'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=edit.php method=post>";
echo "<tr>";
echo "<td><input type=text name=fathername></td>";
echo "</form>";
echo "</table>";
?>
</body>
</html>
You are getting only id now
$query = "SELECT * FROM `member` WHERE `id` =$id"; // will give you all the rows
you're missing a lot of quotes in your strings, you can use \" to echo a double quote without closing the string eg:
echo "<form action=\"modify.php\" method=\"post\">";
Also you shouldn't use unquoted array indexes, while technically this will work,
"SET fathername='$_POST[fathername]'"
its relying on the fact that php will fall back to a string if it can't locate the constant and will produce warnings:
Instead concatenate it like this:
"SET fathername=".$_POST['fathername']."...."
you may have some sql injection vulnerabilities in your code, you should read up on that. and use mysqli_* not mysql_* as it's depreciated
I seem to have a problem with an email form I am making.
the form worked correctly until I added some extra parts to it. however, even when I comment my changes, it won't work.
I thought the problem might be due to some syntax error, but It doesn't seem to be so.
I do not get any error message
below is my code:
<?php
if(isset($_REQUEST['email'])) {
$num=$_REQUEST['num'];
$desc=$_REQUEST['desc'];
$subject="einak";
$nomr=$_REQUEST['nomr'];
$sphr=$_REQUEST['sphr'];
$cylr=$_REQUEST['cylr'];
$angr=$_REQUEST['angr'];
$addr=$_REQUEST['addr'];
$diar=$_REQUEST['diar'];
$noml=$_REQUEST['noml'];
$sphl=$_REQUEST['sphl'];
$cyll=$_REQUEST['cyll'];
$angl=$_REQUEST['angl'];
$addl=$_REQUEST['addl'];
$dial=$_REQUEST['dial'];
$type=$_REQUEST['type'];
$matter=$_REQUEST['matter'];
$color=$_REQUEST['color'];
$message=$num."<br />".$nomr."<br/>".$sphr."<br/>".$cylr."<br/>".$angr."<br />".$addr."<br />".$diar."<br />"."<br />".$noml."<br/>".$sphl."<br/>".$cyll."<br/>".$angl."<br />".$addl."<br />".$dial."<br />".$desc."<br />".$type."<br />".$matter."<br />".$color."<br />";
$email='info#toosoptic.ir';
$headers='MIME-Version: 1.0'."\r\n";
$headers.='Content-type: text/html; charset=iso-8859-1'."\r\n";
mail("mail#example.com",$subject,$message,$headers);
echo "ارسال شما موفقیت آمیز بود";
} else {
echo "<form method='post'>
<input name='num' id='num' type='text' /> <br />
<input type='text' name='nomr' id='nomr' > <br />
<input type='text' name='sphr' id='sphr'> <br />
<input type='text' name='cylr' id='cylr' > <br />
<input type='text' name='angr' id='angr' > <br />
<input type='text' name='addr' id='addr' > <br />
<input type='text' name='diar' id='diar' > <br />
<input type='text' name='noml' id='noml' > <br />
<input type='text' name='sphl' id='sphl'> <br />
<input type='text' name='cyll' id='cyll' > <br />
<input type='text' name='angl' id='angl' > <br />
<input type='text' name='addl' id='addl' > <br />
<input type='text' name='dial' id='dial' > <br />
<textarea id='desc' name='desc'> </textarea>
<input name='type' type='radio' value='bifocal' id='type1' />
<input name='type' type='radio' value='progres' id='type2' />
<input name='type' type='radio' value='single' id='type3' />
<input name='type' type='radio' value='decen' id='type4' />
<input name='matter' type='radio' value='glass' id='mat1' />
<input name='matter' type='radio' value='plastic' id='mat2' />
<input name='color' type='radio' value='single' id='col1' />
<input name='color' type='radio' value='decen' id='col2' />
<input type='submit' id='sb' value='ارسال اطلاعات' />
<input type='text' name='name' id='name' > <br />
<input type='add' name='address' id='address' > <br />
</form >
<p id='confirmation'></p>
<input type='submit' id='vis' onClick='vis(); return false;' value='تایید اطلاعات'/>";
}
?>
You have not define the name in the submit button here:
<input type='submit' id='sb' value='ارسال اطلاعات' />
Replace above code with the below code:
<input type='submit' id='sb' name='email' value='ارسال اطلاعات' />
You are checking the code if(isset($_REQUEST['email'])) and there is no email field the form as I see.
Basically this check is used for verifying that form have submitted or not(as generally other do). So you need to add the name='email' in the submit button attribute to execute the if condition code..
You're setting a mime header but you're not setting any mime encoding, that might be one issue ;-)
Try using a dedicated package such as PEAR/Mail (and PEAR/Mail/Mime) or phpMailer.
I have a customisable quiz site, where the user can input questions and answers. My first PHP file takes the inputted information and generates a quiz page. The answers are in hidden text box. A second PHP then compares the inputted radio button to the hidden text box to see if the user is correct and gives a score in relation to that. I would like to store the questions and answers in a .txt file which could then be accessed by other users, unfortunately as part of the project we are not able to use MySQL. Hopefully I am being clear, I have tried to go about this many ways but all have broken my code. Any ideas would be much appreciated.
My code is below:
INITIAL HTML:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="actions.php" method="post">
<strong>Question 1:</strong> <input name="q1" type="text" /> <br />
Answer 1: <br />
<input name="a11" type="text" /><input name="1" type="radio" value="1.1" /><br />
Answer 2: <br />
<input name="a12" type="text" /><input name="1" type="radio" value="1.2" /><br />
Answer 3: <br />
<input name="a13" type="text" /><input name="1"type="radio" value="1.3" /><br />
Answer 4: <br />
<input name="a14" type="text" /><input name="1" type="radio" value="1.4" />
<br /><br />
<strong>Question 2:</strong> <input name="q2" type="text" /> <br />
Answer 1: <br />
<input name="a21" type="text" /><input name="2" type="radio" value="2.1" /><br />
Answer 2: <br />
<input name="a22" type="text" /><input name="2" type="radio" value="2.2" /><br />
Answer 3: <br />
<input name="a23" type="text" /><input name="2"type="radio" value="2.3" /><br />
Answer 4: <br />
<input name="a24" type="text" /><input name="2" type="radio" value="2.4" />
<br /><br />
<strong>Question 3:</strong> <input name="q3" type="text" /> <br />
Answer 1: <br />
<input name="a31" type="text" /><input name="3" type="radio" value="3.1" /><br />
Answer 2: <br />
<input name="a32" type="text" /><input name="3" type="radio" value="3.2" /><br />
Answer 3: <br />
<input name="a33" type="text" /><input name="3"type="radio" value="3.3" /><br />
Answer 4: <br />
<input name="a34" type="text" /><input name="3" type="radio" value="3.4" />
<input name="" type="submit" />
</form>
</body>
FIRST PHP:
<html>
<body>
<form action="actions2.php" method="post">
<strong><?php echo $_POST["q1"];?></strong>
<br /><br />
<?php
echo $_POST["a11"]."<input name='newa1' type='radio' value='1.1' />"."<br />".$_POST["a12"]."<input name='newa1' type='radio' value='1.2' />"."<br />".$_POST["a13"]."<input name='newa1' type='radio' value='1.3' />"."<br />".$_POST["a14"]."<input name='newa1' type='radio' value='1.4' />"."<br />";
echo "<br /><br />"
?>
<strong><?php echo $_POST["q2"];?></strong>
<br><br>
<?php
echo $_POST["a21"]."<input name='newa2' type='radio' value='2.1' />"."<br />".$_POST["a22"]."<input name='newa2' type='radio' value='2.2' />"."<br />".$_POST["a23"]."<input name='newa2' type='radio' value='2.3' />"."<br />".$_POST["a24"]."<input name='newa2' type='radio' value='2.4' />"."<br />";
echo "<br /><br />"
?>
<strong><?php echo $_POST["q3"];?></strong>
<br><br>
<?php
echo $_POST["a31"]."<input name='newa3' type='radio' value='3.1' />"."<br />".$_POST["a32"]."<input name='newa3' type='radio' value='3.2' />"."<br />".$_POST["a33"]."<input name='newa3' type='radio' value='3.3' />"."<br />".$_POST["a34"]."<input name='newa3' type='radio' value='3.4' />"."<br />";
echo "<br /><br />"
?>
<input name="result1" type="hidden" value="<?php echo $_POST['1']; ?>">
<input name="result2" type="hidden" value="<?php echo $_POST['2']; ?>">
<input name="result3" type="hidden" value="<?php echo $_POST['3']; ?>">
<input name="" type="submit" />
</form>
<br /><br />
</body>
</html>
SECOND PHP:
<?php
$correctAnswer1 = $_POST['result1'];
$newAnswer1 = $_POST['newa1'];
$correctAnswer2 = $_POST['result2'];
$newAnswer2 = $_POST['newa2'];
$correctAnswer3 = $_POST['result3'];
$newAnswer3 = $_POST['newa3'];
$score = 0;
if($newAnswer1 == $correctAnswer1) {
$score++;
}
if($newAnswer2 == $correctAnswer2) {
$score++;
}
if($newAnswer3 == $correctAnswer3) {
$score++;
}
if ($score >= 2)
{
echo "Congratulations, you did well. You got ".$score;
}
else if ($score == 1)
{
echo "Only one, poor show";
}
else
{
echo "YOU GOT ZERO YOU SHITE";
}
?>
I'm not totally sure what you wanted, but if you need a way to store the questions and answers you could look into XML or JSON.
PHP should have libraries to encode/decode both of them
I am not sure if I have understood the question pretty well, but this is how you store in a .txt file using php.
http://bd1.php.net/file_put_contents also take a look at this http://www.w3schools.com/php/func_filesystem_fwrite.asp
As far as I can tell, clicking on submit button does not take me to the next page.
Here is add_product.php which is included in ./admin/index.php and this is the form I have a problem with:
<div class='content'>
<form type='./inc/add_sql.php' method='POST'>
Name: <br/>
<input type='text' name='name' /><br/><br/>
Discription: <br/>
<textarea rows='10' cols='32' name='discription'>
</textarea><br/><br/>
Price (£): <br/>
<input type='text' name='price' /><br/><br/>
Use Stock Values?<br/>
<input type='radio' name='use_stock' value='1' /> Yes  
<input type='radio' name='use_stock' value='0' /> No <br/><br/>
Image Text: <br/>
<input type='text' name='img_alt' /><br/><br/>
Thumbnail Text: <br/>
<input type='text' name='thumb_alt' /><br/><br/>
Stock Level (Optional)<br/>
<input type='int' name='stock' /><br/><br/>
<input type="submit" value="Submit">
</form>
</div>
and add_sql.php (from above)
<?php
include './sql_connect.php';
$name = mysql_real_escape_string($_POST['name']);
$discription = mysql_real_escape_string($_POST['dicription']);
$price = mysql_real_escape_string($_POST['price']);
$use_stock = mysql_real_escape_string($_POST['use_stock']);
$img_alt = mysql_real_escape_string($_POST['img_alt']);
$thumb_alt = mysql_real_escape_string($_POST['thumb_alt']);
$stock = mysql_real_escape_string($_POST['stock']);
$mysql = mysql_query("INSERT INTO `products`(`name`, `dicription`, `price`, `use_stock`, `img_alt`, `thumb_alt`, `stock`) VALUES ({$name},{$discription},{$price},{$use_stock},{$img_alt},{$thumb_alt},{$stock})");
if (!mysql_query($mysql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added <a href='./?add_products'>continue</a>";
mysql_close($con);
?>
can anyone see what is wrong here?
<form type='./inc/add_sql.php'
should be:
<form action='./inc/add_sql.php'
I have a submit buttom like this:
<?php
echo "<form id='abottom' method='post'>
<button name='".$row3[$ww]."' id='".$row3[$ww]."' type='button'>More Details</button>
<input type='hidden' name='action' value='".$row3[$ww]."' />
</form>";
?>
and ids are working right of the buttons of the table rows (by Firebug). But when I want to output $_POST in a query while loop, then no one of those works for me:
<?php
echo $_POST[$row3[$ww]]." <br />";
echo $_POST['$row3[$ww]']." <br />";
echo $_POST[$row3['$ww']]." <br />";
echo $_POST["$row3[$ww]"]." <br />";
echo $_POST[$row3["$ww"]]." <br />";
echo $_POST[$row3['".$ww."']]." <br />";
?>
Which one will be the right? Those above didn't work for me.
$row3 // is a fetch result of sql3
$ww // is table rows name on which one is selected
Raw HTML output example:
<form name ='dets' method='POST'>
<input class = 'showt' name ='6' id ='6' type='button' value= 'More Details'></input>
<input type='hidden' name='data' value='6' />
<noscript><input type='submit' value='Submit'></noscript>
</form>
Much better for debugging and finding the correct variable would be to use
<?php
echo '<pre>';
var_dump($_REQUEST);
echo '</pre>';
to see which variables arrive at your script.
Update:
I think, what you need is:
<form method="post">
<input type="hidden" name="data" value="<?php echo $row3[$ww]; ?>"/>
<input type="submit" value="More Details"/>
</form>
<?php
echo $_POST['data']];
And if you have several values in $row3 then add extra forms like this:
<form method="post">
Second Data
<input type="hidden" name="data" value="<?php echo $row4[$ww]; ?>"/>
<input type="submit" value="More Details"/>
</form>
Always keep the name of the hidden input the same (data)!
Your button has type="button". This should be type="submit" or it won't submit the form it is in.
Your PHP code should look like this:
<?php
echo "<form id='abottom' method='post'>
<button name='".$row3[$ww]."' id='".$row3[$ww]."' type='submit'>More Details</button>
<input type='hidden' name='action' value='".$row3[$ww]."' />
</form>";
?>