Why PHP doesn't receive values from my select? - php

Notice: Undefined index: perm_tipo in C:\xampp\htdocs\admin_reg_usuario.php on line 6
I get this error from my PHP page where is supposed to receive a value from a
html select form, I send this info with POST, this is the code from my select:
<select name="perm_tipo">
<?php
$con=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("test",$con) or die(mysql_error());
$sql = "SELECT id_permiso,tipo_permiso FROM cat_permisos";
$rs = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($rs)){
echo "<option value='".$row["id_permiso"]."'>".$row["tipo_permiso"]."</option>";
}
mysql_free_result($rs);
mysql_close($con);
?>
</select>
and another php page to use the values I use this
$permiso = $_POST['perm_tipo'];
I'm not quite sure what I'm doing wrong, the syntax and name are fine.
EDIT: now is working,the var_dump($_POST); code made it work??, =S

Okay so you're submitting the form to the same PHP page.
That means when you first visit the page, $_POST is empty. After you click submit $_POST is populated when the form is sent to the same page with perm_tipo.
What you need to make sure is that $_POST is sent to your PHP page with isset. This will detect when your form is submitted. This will also not execute the code when $_POST is empty which is why you get that "Notice:" error.
<?php
if (isset($_POST['perm_tipo']))
{
$con=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("test",$con) or die(mysql_error());
$sql = "SELECT id_permiso,tipo_permiso FROM cat_permisos";
$rs = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($rs)){
echo "<option value='".$row["id_permiso"]."'>".$row["tipo_permiso"]."</option>";
}
mysql_free_result($rs);
mysql_close($con);
}
?>

Related

MySQL erase a row based on a populated combobox value

I'm trying to delete a row in mysql based on the selection I make in a combox, I know this is deprecated but it's just for personal use. By the way
I have something like this:
<form method="get" action="">
<?
require ('link.php');
mysql_select_db('proesi',$link) or die(mysql_error());
$rs = mysql_query("SELECT * FROM curso") or die(mysql_error());
echo "<select name='combo'>";
while($row2 = mysql_fetch_array($rs)){
echo "<option value='".$row2["id"]."'>".$row2["curso"]."</option>";
}
echo "</select>";
?>
<input type="submit" name="borrar" value="ELIMINAR" />
</form>
<?
$combos = $_get['combo'];
if (isset ($_post['borrar'])){
print $combos;
require ('link.php');
mysql_select_db('proesi',$link) or die(mysql_error());
mysql_query("DELETE FROM curso WHERE curso= //here i need to get the value, i'm trying to use the "id" field since it is unique// ") or die (mysql_error());
mysql_close($link);
}
?>
the select has a name. When the form is submitted, you can get it's value with $_GET['combo']
Then, in your sql,
$selected_id = $_GET['combo']; //sanitize this!!!
mysql_query("DELETE FROM curso WHERE curso= '$selected_id' ") or die (mysql_error());
of course you have to make sure the value entered into the sql is sanitized and secure from sql injections, but since you are learning, you will get to it later.

Displaying output of php within jquery colorbox ... getting undefined index error

I hope someone can help me, I have created a simple html form with drop down menu's, the drop down menus are populated from a mysql data base, the user must select from two drop downs and this will then display the data ( both selections make the sql query)
This all works correctly within the HTML however I am trying to jazz it up a bit and have the output display within a jquery colorbox (popup).
I am not sure how to format the syntax for the jquery function .. this is what I have so far
<script>
$(document).ready(function(){
$(".inline").colorbox({inline:true, width:"50%"});
$("input#formsubmit").colorbox({href: function(){
var url = $(this).parents('form').attr('action');
return url;
}, innerWidth:920, innerHeight:"86%", iframe:true});
});
</script>
This correctly launches the colorbox pop up and fires the php "action" from my form but the $_POST attributes are not sent across and I just get an unidentified index error from mysql.
Can some one please help me ?
Im sure its something simple, but I cant figure it out.
Many thanks
Adding PHP ...
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("verify") or die(mysql_error());
$result = mysql_query("SELECT Entitlement FROM products WHERE ProductName = '$_POST[product]' AND CustomerType = '$_POST[customer]'")
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['Entitlement'] ;
echo "<br />";
}
?>
Can you please tell how may this code works with you :)
<?php $result = mysql_query("SELECT Entitlement FROM products WHERE ProductName = '$_POST[product]' AND CustomerType = '$_POST[customer]'");?>
You have 2 error the first one the single quote beside the index in your post variable and must be like this $_POST['product'],$_POST['customer'] then the second error is that you must encapsulate your variable inside the string as following {$_POST['product']},{$_POST['customer']}
Try these work around then tell me the result :)
try this
<?php
// Checking for valid post data
if (isset($_POST['product']) && isset($_POST['customer']) && !empty($_POST['product']) && !empty($_POST['customer'])) {
// Cleaning post data
$proudct = mysql_escape_string($_POST['product']);
$customer = mysql_escape_string($_POST['customer']);
// db connnection
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("verify") or die(mysql_error());
// Quering
$result = mysql_query("SELECT Entitlement FROM products WHERE ProductName = '$proudct' AND CustomerType = '$customer'") or die(mysql_error());
// Printing result
while ($row = mysql_fetch_array($result)) {
echo $row['Entitlement'];
echo "<br />";
}
}
?>

Load values from database to create a drop down menu based on the selection from previous dropdown

My problem is that I try to load a drop down based on selection from previous drop down. I'm loading the values from database. but when I use $_get['emirate'] I'm not getting the value. It says the value is not set.
$data = mysql_query("SELECT * FROM emirate")
or die(mysql_error());
Print "<table border cellpadding=3>";
print"<tr><th><form method=get action='index.php'><select id=EMIRATE size=1><OPTION value=all>all</option>";
while($info = mysql_fetch_array( $data ))
{
Print "<option value=". $info['em_name'] .">".$info['em_name']."</option>";
}
print"</select></form></th>";
if(isset($_GET['EMIRATE'])){
$name=$_GET['EMIRATE'];
echo $name;
$data = mysql_query("SELECT a_name FROM areas where em_id=(select em_id from emirate where em_name=\"".$name."\")")
or die(mysql_error());
Print "<th><select name=AREAS size=1>";
while($info = mysql_fetch_array( $data ))
{
Print "<option >".$info['a_name']."</option>";
}
print"</select></th>";
}
I found out the problem.
I haven't put a submit button and hence my form wasn't getting
submitted. And hence I wasn't getting any value from $_GET.
http://php.net/manual/en/reserved.variables.get.php
Now I get the results smoothly.
which method are you using on your form on the previous page, post or get?
you can replace $_GET with $_REQUEST which retrieves values from both post and get.
<select id=EMIRATE size=1><OPTION value=all>all</option>
Please fix the HTML.... you miss to set the name attribute for your select...
<select id="EMIRATE" size="1" name="EMIRATE">
<option value="all">all</option>
....

Some PHP Code is breaking the rest of the page

I have an odd problem. Basicly my page works fine however after a small bit of php, everything after those lines dont load too the page.
<?php
//GET SCHOOLS
$sql = "SELECT `id` FROM `school`";
$query = mysql_query($sql) or die(mysql_error());
$numschools = mysql_num_rows($query);
echo "<select id=\"schoolselect\" class=\"schoolselect\" value=\"Select School\">
<option id='selectschool' value = \"select\" name=\"select\">Select A School</option>
";
while($result = mysql_fetch_array($query) or die(mysql_error()))
{
$school = $result['id'];
echo "<option value = \"$school\" name=\"$school\">".$school ."</option>";
}
echo "</select>";
?>
everything before that works, the php part works but anything after that echo "select" doesnt
any help would be amazing
Never call die(mysql_error()) inside a fetch loop. mysql_fetch_array() returns FALSE when no more rows are available, so when the last row is reached, die() is called and your script will terminate leaving the </select> unclosed. You won't get an error message, but you'll be left with incomplete HTML that won't render properly in the browser.
// Don't call die(mysql_error()) in a fetch loop!
while($result = mysql_fetch_array($query))
{
$school = $result['id'];
echo "<option value = \"$school\" name=\"$school\">".$school ."</option>";
}
Chances are your script is or die()ing inside the <select> tag, which is invisible in the browser.
View the page's source, and you'll most likely see a PHP fatal error or a die message at the end.
EDIT: Or read Michael's answer, cuz I forgot how things worked. Duh :p At least this answer will help you find related problems in the future.

Refreshing form -> uploading data

I have a problem with my PHP form. Whenever I refresh the page, the old data automatically inserted to my database. My codes are:
<?php
if(isset($_GET['send'])){
isset($_GET['name'])?$name = $_GET['name']:"";
isset($_GET['score'])?$score = $_GET['score']:0;
$con = mysql_connect('localhost','root','') or die(mysql_error());
mysql_select-db('student',$con) or die(mysql_error());
$qry = mysql_query('INSERT INTO student(name, score) VALUES('$name', $score)') or die(mysql_error());
$display = mysql_query('SELECT * FROM student',$con) or die(mysql_error());
echo '<table border=1>';
while($rows = mysql_fetch_array($display)){
echo '<tr>';
echo "<td>$rows['id']</td>";
echo "<td>$rows['name']</td>";
echo "<td>$rows['score']</td>";
echo '</tr>';
}
echo '</table>';
}
?>
please help me solve this problem.
A common way to prevent duplicate form submission is to make use of the Post/Redirect/Get Pattern.
You would need to change your forms method to Post then. After successful form submission you redirect to the form again but making the redirect a get request. The form will be reset then (empty values).
Edit:
Now as I see it, your script can actually do something similar: After insertion into the mysql Database you can redirect it to itself removing the get parameters:
<?php
if(isset($_GET['send'])){
isset($_GET['name'])?$name = $_GET['name']:"";
isset($_GET['score'])?$score = $_GET['score']:0;
$con = mysql_connect('localhost','root','') or die(mysql_error());
mysql_select-db('student',$con) or die(mysql_error());
$qry = mysql_query('INSERT INTO student(name, score) VALUES('$name', $score)') or die(mysql_error());
header('Location: myscriptsurl.php');
exit;
}
$display = mysql_query('SELECT * FROM student',$con) or die(mysql_error());
echo '<table border=1>';
while($rows = mysql_fetch_array($display)){
echo '<tr>';
echo "<td>$rows['id']</td>";
echo "<td>$rows['name']</td>";
echo "<td>$rows['score']</td>";
echo '</tr>';
}
echo '</table>';
?>
So you have a problem.
And since you cannot avoid a refresh of the screen...
If you are doing a form post, you might consider sending a location
header AFTER you inserted the record:
<form action="process.php" method="POST">
<input type="text" name="number">
<input type="sumbit">
</form>
then from process.php:
// do you usual inserts in the database based on the post
header("Location: http://www.example.com/thanks.php");
// do not forget the exit, since your script will run on without it.
exit;
In that way your script will process the posting, and then redirects the
browser to thanks.php.
A reload of thanks.php will not result in a fresh db insert.
U have used GET method so every time page refresh it will fetch the value from URL.
Try using POST method...It will solve your Problem and don't forget to Put Condition for POST
if(isset($_POST))
{
/* Your Insert Code */
}

Categories