We are getting the values from the select and then trying to print them on a textbox so we can edit it. But when test it, the echo on the value just returns the first word of the sentence that is inside the column. For example our row[2] is the column "Nombre" with the value "estadio nacional", when we print it, the echo only return "estadio".
<!DOCTYPE html>
<html>
<head>
<title>Editar Estadio</title>
</head>
<body>
<h1>Editar Estadios</h1>
<?php
// Conectando y seleccionado la base de datos
$dbconn = pg_connect("host=localhost dbname=tarea3 user=postgres password=12345")
or die('No se ha podido conectar: ' . pg_last_error());
$id_1 = $_GET["id"];
$result=pg_query("SELECT * FROM estadio where id_estadio='".$id_1."'");
$row=pg_fetch_row($result, 0, PGSQL_NUM);
echo $row[2];
$nombre = $row[2];
?>
<form method ='post' action='checkaddestadio.php'>
<pre>
Nombre: <input type='text' name='Nombre' value=<?php echo $nombre ?>>
Capacidad: <input type='text' name='Capacidad' value=<?php echo $row[3] ?>>
Ciudad: <input type='text' name='Ciudad' value=<?php echo $row[4] ?>>
Direccion: <input type='text' name='Direccion' value=<?php echo $row[5] ?>>
Descripcion: <input type='text' name='Descripcion' value=<?php echo $row[6] ?>>
Fotografia: <input type='text' name='Fotografia' value=<?php echo $row[1] ?>>
</pre>
<br><input type='submit' value='Ingresar'></br>
</form>
<?php
// Cerrando la conexión
pg_close($dbconn);
?>
</body>
</html>
You need to use quotes around your HTML element attributes. As it stand, the HTML generated from your code would look like this
<input type='text' name='Nombre' value=estadio nacional>
See the problem?
You should also use htmlspecialchars() to avoid any XSS vulnerabilities or general HTML incorrect-ness, eg
<input type="text" name="Nombre" value="<?= htmlspecialchars($nombre) ?>">
Also, why the <pre> tags?
Related
I have just written this code in which I have a form. You have to write your name, surname and country. You also have to choose your favourite colour. After that, you push a submit button so that you can see the data afterwards. I'm using the GET method with 1 page, but I have to use a second one with the POST method so that each echo is on that second page.
How could I do that? My code is:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form</title>
<link rel="stylesheet" type="text/css" href="form.css">
</head>
<body>
<section>
<?php
if (isset($_GET["name"])){
$name = $_GET["name"];
if ($name != ""){
$surname = $_GET["surname"];
$country = $_GET["country"];
$colour = $_GET["colour"];
echo "<p>";
echo "<h2>Data</h2>";
echo $name . " " . $surname . "</br />";
echo $country . "<br />";
echo $colour;
echo "</p>";
}else
echo "<strong>Complete the blank spaces</strong>";
}else{
?>
<h1>Form</h1>
<form class="elegant" method="GET" action="?">
<fieldset>
<legend>Favourite colour</legend>
<div>
<label for="nombre">Name</label>
<input type="text" placeholder="Name" name="name"
id="name" />
</div>
<div>
<label for="surname">Surname</label>
<input type="text" placeholder="Surname" name="surname"
id="surname" size="50" />
</div>
<div>
<label for="country">Country</label>
<input type="text" placeholder="Country" name="country" id="country"
size="10" maxlength="9" />
</div>
<div>
<select name="colour" id="colour">
<option value="yellow" <?php if ($colour == "yellow" ) echo "selected" ?> >yellow</option>
<option value="red" <?php if ($colour == "red" ) echo "selected" ?> >red</option>
</div>
<input class="btn" type="submit" value="Save" />
</fieldset>
</form>
<?php
}
?>
</section>
</body>
</html>
I know i have to use a link to that second page, but that's all I know. Thanks in advance!
If I understand correctly, the code you show is for Page 1 where the user can:
Input data if data don't exist;
View data and confirm them if they exist. At this point store data in SESSION and send the user to an other page.
To do so, remember you have to add the session_start() command right at the beginning of every page in which you will be able to manipulate Session Data.
<?php
session_start();
if (isset($_GET["name"])){
$name = $_GET["name"];
if ($name != ""){
$surname = $_GET["surname"];
$country = $_GET["country"];
$colour = $_GET["colour"];
echo "<p>";
echo "<h2>Data</h2>";
echo $name . " " . $surname . "</br />";
echo $country . "<br />";
echo $colour;
echo "</p>";
$_SESSION["name"] = $name;
$_SESSION["surname"] = $surname;
$_SESSION["country"] = $country;
$_SESSION["colour"] = $colour;
Confirm
}else
echo "<strong>Complete the blank spaces</strong>";
}else{
?>
...
In "another_page.php" then you will find that you can access your data simply querying the $_SESSION array.
<?php
session_start();
...
// Echo session variables that were set on previous page
echo "Name is " . $_SESSION["name"] . ".<br>";
echo "Surname is " . $_SESSION["surname"] . ".";
// etc.
?>
Complete reference is the PHP Manual and simple reference is in W3C PHP Sessions page.
In the form tag you can specify where to form gets submitted to:
<form class="elegant" method="GET" action="YOUR_PAGE_URL_HERE">
On that second page you get the values via the $_GET Variable like
<?php
echo $_GET['name'].' '.$_GET['surname'];
I'm trying to capture question ids and their answers. The problem
is that the value is "ON" when I print the array, it has the
question number correctly in the first cell but then the value in the
second cell is "ON". Here is my code:
function get_questions($quiz_id)
{
include'connection.php';
$stmt = $conn->prepare("select id,question,option1,option2,option3,option4,answer from questions where quiz_id=?");
$stmt->bind_param("i",$quiz_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($qid,$question,$option1,$option2,$option3,$option4,$answer);
$num_of_rows = $stmt->num_rows;
while($stmt->fetch()) {
echo $question;
echo "<br/>";
?>
<form method="POST"action="">
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="x"><?php echo $option1;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="y"><?php echo $option2;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="z"><?php echo $option3;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="f"><?php echo $option4;?><br/>
<!--<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="<?php echo $option2;?>"><?php echo $option2;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="<?php echo $option3;?>"><?php echo $option3;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="<?php echo $option4;?>"><?php echo $option4;?><br/>
-->
<?php
}
?>
<input type="submit" name="submit" value="submit" >
</form>
<?php
if(isset($_POST['submit'])) {
$answers=$_POST['radio'];
print_r($answers);
// Iterate through each answer
}
}
Your problem is a simple syntax error:
]value="x">
This square bracket is changing the name of the attribute from value to ]value, so there is no value to be captured upon submission.
Also, make sure to move the opening form tag outside of your loop:
while($stmt->fetch()) {
...
<form method="POST"action=""> // <- this here opens a new tag with each iteration
because you close the tag outside the loop, meaning you're not creating valid html.
And while we're here, I'd suggest the alternative syntax when you have mixed php and html code:
<form method="POST" action="">
<?php while ($stmt->fetch()): ?>
<input type="radio"...
...
<?php endwhile ?>
<input type="submit" name="submit" value="submit" >
</form>
It makes it more readable and easier to follow than having to match curly braces.
I am a complete beginner, so please bear with me. This is just a test project I am putting together to try to teach myself some of the basics.
I know that a lot of my commands are outdated and/or susceptible to injection, but I'd rather stick with this for now (many reasons).
I just had a question about trying to use SELECT from WHILE, and figured that out and got it to echo the correct response on the page.
Now, how do I make it echo that as a value for an HTML text box? It won't work, and I've tried to look for typos but I don't know what I am doing, frankly.
I see that the $studentid and $teacherinfo show fine, I presume because they are normal variables.
Can I somehow define two more variables for first name and last name further up in the page so that I do not need to include so much code in each input (and to keep it from being buggy)?
Here is my code for the page. The inputs will be hidden, but I have been making them text boxes for debugging purposes.
<?php
$connection = mysql_connect($serverName, $userName, $password) or die('Unable to connect to Database host' . mysql_error());
$dbselect = mysql_select_db($dbname, $connection) or die("Unable to select database:$dbname" . mysql_error());
$studentid = $_POST['student_id'];
$teacherinfo = $_POST['teacher'];
$result = mysql_query("SELECT `first_name` FROM `students` WHERE student_id = '$studentid'",$connection);
?>
</head>
<body>
<div align="center">
<form method="post" action="vote_post.php">
<h1>Vote for Teacher of the Month</h1>
<h4>(step 2 of 2)</h4>
<h2>Confirm the Information Below</h2>
<h5>Student id: <?php echo $studentid ?></br>
Student first name: <?php
while($row = mysql_fetch_array($result)){
echo $row['first_name'];
}
?>
</br>
Voted for: <?php echo $teacherinfo ?>
</h5>
<input type="text" name="student_id" value="<?php echo $studentid; ?>"/></br>
<input type="text" name="first_name" value="<?php while($row = mysql_fetch_array($result)){
echo $row['first_name'];
} ?>"/>
</br>
<input type="text" name="last_name" value="<?php while($row = mysql_fetch_array($result)){
echo $row['last_name'];
} ?>"/>
</br>
<input type="text" name="teacher" value="<?php echo $teacherinfo; ?>"/></br>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
</form>
You can't use while because your query return only one student. You have to use if instead of while. If your query return many students you can use while.
Try this code:
<?php
if($row = mysql_fetch_array($result)){
?>
Student first name: <?php echo $row['first_name'];?>
</br>
Voted for: <?php echo $teacherinfo ?></h5>
<input type="text" name="student_id" value="<?php echo $studentid; ?>"/></br>
<input type="text" name="first_name" value="<?php echo $row['first_name'];?>"/></br>
<input type="text" name="last_name" value="<?php echo $row['last_name'];?>"/></br>
<input type="text" name="teacher" value="<?php echo $teacherinfo; ?>"/></br>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
<?php
}
?>
I hope this help.
I tried to build a code that will help you. Remember that you use last_name, but does not return the field in SQL.
</head>
<body>
<div align="center">
<form method="post" action="vote_post.php">
<h1>Vote for Teacher of the Month</h1>
<h4>(step 2 of 2)</h4>
<h2>Confirm the Information Below</h2>
<?php
$connection = mysql_connect($serverName, $userName, $password) or die('Unable to connect to Database host' . mysql_error());
$dbselect = mysql_select_db($dbname, $connection) or die("Unable to select database:$dbname" . mysql_error());
$studentid = $_POST['student_id'];
$teacherinfo = $_POST['teacher'];
$result = mysql_query("SELECT `first_name`,`last_name`,`student_id` FROM `students` WHERE student_id = $studentid",$connection);
while($row = mysql_fetch_array($result)){
echo "<h5>Student id: $row['student_id'] </br>" .
"Student first name: $row['first_name'] </br>" .
"Voted for: $teacherinfo </h5> " .
"<input type='text' name='student_id' value='$row[\'student_id\']' /></br>" .
"<input type='text' name='first_name' value='$row[\'first_name\']' /></br>" .
"<input type='text' name='last_name' value='$row[\'last_name\']' /></br>" .
"<input type='text' name='teacher' value='$teacherinfo' /></br>"
}
?>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
</form>
WHILE I left because I do not know if your query can return more than one record, despite appearing to be a key. If you do not need to check the response of the Ragnar.
I've the following code :
$query = "SELECT * FROM items WHERE SUBSTRING(item_no, 1, ".$length.") BETWEEN
'".$from_new."' AND '".$to_new."' ORDER BY item_no Desc";
$result = mysql_query($query);
$dd=array();
$ii=array();
$qq=array();
$aa=array();
if(mysql_num_rows($result)>0){
$num = mysql_num_rows($result);
?>
<form method="post" action="final_group_items.php">
<?php
echo "<table>";
for($i=0;$i<$num;$i++){
$row = mysql_fetch_array($result);
echo "<tr><td align=center>"; ?>
<input disabled maxlength="2" type="text"
name="ii[]" value="<?php echo strtoupper($row['item_no']); ?>"><?php echo
"</td><td align=center>";?>
<input disabled maxlength="2" type="text"
name="qq[]" value="<?php echo $row['qty'];?>">
<?php echo "</td><td align=center>"; ?>
<input disabled maxlength="2" type="text"
name="aa[]" value="<?php echo $row['actual_price'];?>">
<?php echo "</td><td align=center>";?>
<input required maxlength="2" type="text" name="dd[]" value="<?php echo
$row['discount_price']; ?>">
<?php
echo "</td><tr>";
}
echo "</table>";
?>
<input type="submit" value="Change Values">
</form>
Now when i click Submit it will open the final_group_items.php which has the follow testing code to make sure if all array (ii,qq,dd,aa) are not empty:
if(empty($_POST['qq']))
{
echo "No value inside";
return false;
}
foreach($_POST['qq'] as $test)
{
echo $test;
}
return true;
so by testing all array's, the only one works is $_POST['dd']...Others outputs "no value inside" which I really don't know how or why?
What should I do when I have multiple of fields having uniqe arrays and values.
Thank You
This:
<input disabled maxlength="2" type="text" name="qq[]" value="<?php echo $row['qty'];?>">
Will prevent the browser from even posting the field at form submission, because disabled gets an implicit value of "disabled".
The attribute should be removed:
<input maxlength="2" type="text" name="qq[]" value="<?php echo $row['qty'];?>">
Can you tell me why when I click call my data is not posting to my action?
<body>
<?php
$login = '1236567';
$password = '10152930';
$officeNumber = array('0212177899','027899899','09111');
?>
<div id="wrapper">
<form method="POST" action="https://live.domain.co.nz/call.php?login=<?php echo $login; ?>&password=<?php echo $password; ?>&aparty=<?php echo $number; ?>phone&bparty=<?php echo $number;?>">
<?php
echo '<label for="officeNumbers">Office Number: </label>';
echo '<select name="officeNumbers">';
foreach($officeNumber as $number)
{
echo '<option value="'.$number.'">'.$number.'</option>';
}
echo '</select>';
?>
<label for="callTo">Call: </label><input type="text" id="callTo">
<input type="submit" value="Call">
</form>
</div>
</body>
</html>
Your text input:
<input type="text" id="callTo">
… has no name attribute, so it cannot be a successful control (and thus submit any data).
Give it a name attribute.