PHP coding: cannot find why it does not work - php

I am writing a program that is supposed to add a studio. This is my addstudio.php
<?php
include ("db.php");
$pagename="Add a New Location";
echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";
echo "<body>";
echo "<hr><b>".date('d F Y H:i:s')."</b>";
echo "<hr>Fill the form below to add a new studio";
//create a html form to capture user input
echo "<form method=post action=getstudio.php>" ;
echo "<table border=0 cellpadding=10>";
echo "<tr><td>*Studio Id </td>";
echo "<td><input type=text name=form_studioID size=35></td></tr>";
echo "<tr><td>*Studio Name </td>";
echo "<td><input type=text name=form_studioName size=35></td></tr>";
echo "<tr><td>*Address </td>";
echo "<td><input type=text name=form_studioAddress size=35></td></tr>";
echo "<tr><td>*Post Code </td>";
echo "<td><input type=text name=form_postcode size=35></td></tr>";
echo "<tr><td>Telephone Number </td>";
echo "<td><input type=text name=form_tel size=35></td></tr>";
echo "<tr><td><input type=submit value='Add Studio'></td>";
echo "<td><input type=reset value='Clear Form'></td></tr>";
echo "</table>";
echo "</form>" ;
echo "</body>";
?>
And then the getstudio.php file that is supposed to add the entered studio to the database and display all of them(the one that has been just entered and these that already were on the server)
<?php
include ("db.php");
$pagename="View Studio";
echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";
echo "<body>";
echo "<hr><b>".date('d F Y H:i:s')."</b>";
echo"<hr>";
//capture the values inserted in the form's fields and store them in local variables
$studid=$_POST['form_studioID'];
$studname=$_POST['form_studioName'];
$studaddress=$_POST['form_studioAddress'];
$studpostcode=$_POST['form_postoce'];
$studtel=$_POST['form_tel'];
//check if any if the mandatory fields were not filled in
if(empty($studid) or empty($studname) or empty($studaddress) or empty($studpostoce)
or empty($studtel)){
echo "<p> Please ensure all fields are filled in !";
}
else{
$addstudioSQL=
"insert into
studio(studio_id, studio_name, studio_address, studio_postcode, studio_tel)
values(".$studid.", '".$studname."', '".$studaddress."', '".$studpostcode."', '".$studtel."')";
}
$exeaddstudioSQL=mysql_query($addstudioSQL);
//write SQL query
$viewstudioSQL="select studio_id, studio_name, studio_address, studio_postcode, studio_tel
from studio
order by studio_id";
//Run SQL query or exit if any errors are retrieved
$exeviewstudioSQL=mysql_query($viewstudioSQL) or die (mysql_error());
//Create an array of records and fecth the results of the execution of the SQL query
//Loop through the array of records and display details in specific format
while ($arraystudio=mysql_fetch_array($exeviewstudioSQL))
{
echo "<p>Location Id: ".$arraystudio['studio_id'];
echo "<br>".$arraystudio['studio_name']."<br> ".$arraystudio['studio_address'].", ".$arraystudio['studio_postcode'];
echo "<br>".$arraystudio['studio_tel'];
echo "<hr>";
}
echo "</body>";
?>
What this does is to print the studios that are on the server but not the one that has been just entered. It also has some problems with printing the postcode.
Anyone who can help? Much appreciated.

Related

PHP MySql update query not working,says:- Undefined Index [duplicate]

This question already has answers here:
How to get input field value using PHP
(7 answers)
Closed 6 years ago.
I have follow the tutorial of it where i want to update my database using two php files.
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action =update.php method=post>";
echo "<td><input type=text name=Cname value='".$row['CustomerName']."'></td>";
echo "<td><input type=number name=size min=1 value='".$row['TableSize']."'></td>";
echo "<td><input type=date name=Adate value='".$row['DateA']."'></td>";
echo "<td><input type=time name=Atime value='".$row['TimeA']."'></td>";
echo "<td><input type=tel name=phonenumber value='".$row['PhoneNumber']."'></td>";
echo "<input type=hidden name=id value='".$row['TableID']."'>";
echo "<td><input type=submit>";
echo"</form></tr>";
}
?>
this is what i use for the first php file
as for the update.php:
<?php
$con = mysqli_connect('127.0.0.1','root','');
mysqli_select_db($con,'restaurant');
$sql = "UPDATE addtable SET CustomerName='$_POST[Cname]', TableSize='$_POST[size]', DateA='$_POST[Adate]',TimeA='$_POST[Atime]',PhoneNumber='$_POST[phonenumber]', WHERE TableID=$_POST[id]";
if(mysqli_query($con,$sql))
header("refresh:1; url=AssignBooking.php");
else
echo "Not Update";
?>
but the $sql line just doesn't work as it says that
Undefined index: Cname and other indexes too.
put quotes outside the post variable:
$sql = "UPDATE addtable SET CustomerName='".$_POST['Cname']."', TableSize='".$_POST['size']."', DateA='".$_POST['Adate']."',TimeA='".$_POST['Atime']."',PhoneNumber='".$_POST['phonenumber']."', WHERE TableID=".$_POST['id'];
According to your code put the name attributes value ' single quote.
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action =update.php method=post>";
echo "<td><input type=text name='Cname' value='".$row['CustomerName']."'></td>";
echo "<td><input type=number name='size' min=1 value='".$row['TableSize']."'></td>";
echo "<td><input type=date name='Adate' value='".$row['DateA']."'></td>";
echo "<td><input type=time name='Atime' value='".$row['TimeA']."'></td>";
echo "<td><input type=tel name='phonenumber' value='".$row['PhoneNumber']."'></td>";
echo "<input type=hidden name="id" value='".$row['TableID']."'>";
echo "<td><input type=submit>";
echo"</form></tr>";
}
?>
Put quotes accordingly
UPDATE addtable SET CustomerName='".$_POST['Cname']."',TableSize='".$_POST['size']."', DateA='".$_POST['Adate']."',TimeA='".$_POST['Atime']."',PhoneNumber='".$_POST['phonenumber']."' WHERE TableID=$_POST['id'];

PHP Basket quantity variable

I am trying to create a php page where the materials from database are populated. Users should be able to enter the quantity next to the item they wish to order and I have created a qty text field for this
<?php
session_start();
include("db.php");
$pagename="Order Material";
echo "<html>";
echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";
include ("detectlogin.php");
echo "<link rel=stylesheet type=text/css href=mystylesheet.css>";
$sql="select * from material";
$result=mysqli_query($con, $sql) or die(mysqli_error($con));
echo "<table border=1>";
echo "<tr>";
echo "<th>Material Name</th>";
echo "<th>Material Description</th>";
echo "<th>Toxicity Level</th>";
echo "</tr>";
while ($arraymaterials=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$arraymaterials['materialName']."</td>";
echo "<td>".$arraymaterials['materialDescrip']."</td>";
echo "<td>".$arraymaterials['materialToxicity']."</td>";
echo "<td>Enter Quantity</td>";
echo "<td><input type=text name=qty value=qty size=5></td>";
echo "<form action=request_material.php method=post>";
echo "<input type=hidden name=materialcode value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
?>
However, I cannot successfully post the value of qty on to the next page even though I have $qty=$_POST['qty']; on my request_material.php. Do you know why this value entered in the qty field cannot be posted onto the request_material.php page? Do I need a session variable?
thanks
Because input tag name="qty" is outside the form tag
echo "<td><input type=text name=qty value=qty size=5></td>";// outside form tag
echo "<form action=request_material.php method=post>";
echo "<input type=hidden name=materialcode value=" . $arraymaterials['materialCode'] . ">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
You need to add it inside your form tag as
echo "<form action=request_material.php method=post>";
echo "<td><input type=text name=qty value=qty size=5></td>";// add inside it
echo "<input type=hidden name=materialcode value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
Please try this: I have updated the code:
echo "<table border=1>";
echo "<tr>";
echo "<th>Material Name</th>";
echo "<th>Material Description</th>";
echo "<th>Toxicity Level</th>";
echo "</tr>";
if(mysqli_num_rows($result)>0)
{
echo "<form action=request_material.php method=post>";
while ($arraymaterials=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$arraymaterials['materialName']."</td>";
echo "<td>".$arraymaterials['materialDescrip']."</td>";
echo "<td>".$arraymaterials['materialToxicity']."</td>";
echo "<td>Enter Quantity</td>";
echo "<td><input type='text' name='qty[]' value='qty' size=5></td>";
echo "<input type=hidden name='materialcode[]' value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</tr>";
}
echo "</form>";
}
echo "</table>";
In request_material.php check value of $qty.It will be an array.for more details print_r($_POST) in request_material.php

button links in html table for every rows ,passing parameter id to row

In my HTML table I have created a button link for each and every row . I need to edit existing rows and add new rows. The links are working perfectly but I need to pass parameters to url .Like for example I f I need to add new rows I need to get the values of the data cells like
echo "<form action=insert_rows.php?machine_ip&crawler_type&keywords&instances_no method=get>";
Its going to the proper link but I am not able to the row data ,its not getting passed, same with existing rows, through submit button I am able submit to the link but I am not able to pass the row values.For existing rows I need to pass the respective primary keys and then access the rows.
My code so far
<style>
input{
width: 100%
}
</style>
<?php
include('db_connect.php');
$conn = db_connect();
mysql_select_db("crawler_status");
$query="Select * from crawler_info";
$result=mysql_query($query);
echo "<table style=width:1000px border=2>";
echo "<tr>";
echo "<td>"."<b>"."Machine IP"."<b>"."</td>";
echo "<td>"."<b>"."Crawler Type"."</b>"."</td>";
echo "<td>"."<b>"."Keywords"."</b>"."</td>";
echo "<td>"."<b>"."No Of Instances"."</b>"."</td>";
echo "<td>"."<b>"."No Of Keywords"."</b>"."</td>";
echo "<td>"."<b>"."Running Status"."</b>"."</td>";
echo "</tr>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<form action=individual_rows.php method=get>";
echo"<td>". $row['machine_ip']."</td>";
echo "<td>". $row['crawler_type']."</td>";
echo "<td>". $row['keywords']."</td>";
echo "<td>". $row['no_of_instances']."</td>";
echo "<td>". $row['no_of_keywords']."</td>";
echo "<td>". $row['running_status']."</td>";
echo "<td>"."<input type=submit value =EDIT></td>";
echo "</form>";
echo "</tr>";
}
echo "<tr>";
echo "<form action=insert_rows.php?machine_ip&crawler_type&keywords&instances_no method=get>";
echo "<td>"."<input type=text name=machine_ip form=my_form></td>";
echo "<td>"."<input type=text name=crawler_type form=my_form ></td>";
echo "<td>"."<input type=text name=keywords form=my_form></td>";
echo "<td>"."<input type=text name= =instances_no></td>";
echo "<td>"."<input type=text name=keywords_no></td>";
echo "<td>"."<input type=submit value =submit></td>";
echo "</form>";
echo "</tr>";
echo "</table>";
?>
How should I proceed?
action=insert_rows.php?machine_ip&crawler_type&keywords&instances_no
According to above when you are submitting form then action URL get truncated ?machine_ip&crawler_type&keywords&instances_no and only you have working action URL is action=insert_rows.php So according to me you should use hidden field to send data. like
<form action ='action=insert_rows.php' method=get>
<input type=hidden name=machine_ip value= ''>
<input type=hidden name=crawler_type value= ''>
<input type=hidden name=keyword value= ''>
<input type=hidden name=instances_no value= ''>
</form>
where value has your desired value.
You can try hidden field to pass data.
Use
<input type="hidden" name="machine_ip" value="<?php echo $row['machine_ip'] ?>" />
same may be applied for other field.

HTML button running out of alignment

My idea is very simple, I will have a search box and a submit button.
When user key in the keyword and click on the submit button, results will be shown below with an additional button. Now my problem is I have no idea on how to make the button to be located at bottom right of the table populated.
Please consider the below code for my situation:
<input type="text" name="criteriaInput" style="width: 300px;"> <input type="submit" name="submit" value="GO" />
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['inquiryMethod'])){
error_reporting(0);
$sql = 'SELECT
*
FROM
table
WHERE
fullname REGEXP \''.$_POST['criteriaInput'].'\'' ;
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("mysql",$server);
$query = mysql_query($sql);
echo "<table class=\"striped\">";
echo "<tr class=\"header\">";
echo "<td>Full Name</td>";
echo "<td>ID</td>";
echo "<td>ID Type</td>";
echo "<td>Issuance Country</td>";
echo "<td>Class</td>";
echo "</tr>";
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[fullname]."</td>";
echo "<td>".$row[id]."</td>";
echo "<td>".$row[id_type]."</td>";
echo "<td>".$row[issuance_country]."</td>";
echo "<td>{$row['class']}</td>";
echo "</tr>";
}
echo "<form method=\"post\" action=\"CIF_InquiryAction.php\">";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
echo "</form>";
echo "</table>";
}else{
echo "Please select one of the criteria!";
}
}
?>
The submit button with value "Create" did successfully created on existence of data, however it's aligned on top left of the table.
Kindly advice Thank you.
You need to put your button into a table row and cell.
echo "<tr>";
echo "<td colspan=\"5\">"
echo "<form method=\"post\" action=\"CIF_InquiryAction.php\">";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
echo "</form>";
echo "</td>"
echo "</tr>";
Also, your form should probably move to be outside your table.
Editing to show input outside of table:
echo "</table>";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";

PHP - How do you select a specific index of a row given by mysqli_fetch_array()?

Please refer to the image below:
http://i.stack.imgur.com/6hBPC.png
For instance, if a user clicks the button on the row which says "You have a quiz for math", the "Quiz ID" value of THAT row would then be passed to another PHP file.
Here's my current code:
<?php
$con=mysqli_connect("127.0.0.1", "root", "", "quizmaker");
if (mysqli_connect_errno($con))
{
echo "MySqli Error: " . mysqli_connect_error();
}
$now=date("m/d/Y");
$sql=mysqli_query($con,"SELECT * FROM quiz_query WHERE quiz_date='$now'");
$count=mysqli_num_rows($sql);
if($count>=1)
{
echo "<table border='1' width='50%'>";
echo "<form action='answer_quiz.php' method='post'>";
echo "<tr>
<td>You have a pending quiz!</td><td> </td><td> </td>
</tr>";
$number=1;
while($result=mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>You have a quiz for " . $result['subject'] . "</td>";
echo "<td>Quiz ID: " .$result['quiz_ID']. "</td>";
echo "<td><input type='submit' name='button' id='button' value='Take Quiz'>";
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>";
echo "</td>";
echo "</tr>";
$number++;
}
echo "</form>";
echo "</table>";
}
else
{
"You have no quiz! :D";
}
mysqli_close($con);
?>
Move this line:
echo "<form action='answer_quiz.php' method='post'>";
Inside of the while loop.
Also, change
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>"
with
echo "<input type='hidden' name='quizId' value='$result[quiz_ID]'>"
Now, in answer_quiz.php you'll receive $_POST['quizId'] with the value you need.
Change your while to :
while( $row = $result->fetch_array(MYSQLI_ASSOC)){
echo $row['subject'];
}
You are forgetting quotes around your variable:
Instead of
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>";
It should be
echo "<input type='hidden' name='quiz[$number]' value='$result[\"quiz_ID\"]'>";

Categories