Jump to content

POST,GET, select etc...


xox

Recommended Posts

I have dropdown populated from mysql and now I want to add ID of selected value into other table, it doesn't get the ID.

 

here's the code I have

if (isset($_REQUEST['subcat']))
{
$id_main = $_GET['categoriesID'];
$DB-> Query('INSERT INTO subcat(id_main_cat,name_subcat) VALUES ("'.$id_main.'","'.$newSub.'")');
}
?>
<br />
	<?php
	$result = mysql_query("SELECT id,name_cat FROM category") 
                                or die(mysql_error());   
		echo "Pick main:";
		echo "<select name='categoriesID'>";
             //reads from table 'categories'
             while($row = mysql_fetch_array( $result )) {
             // display them in dropdown
		 echo '<option value="'.$row['id'].'">';
             echo $row['id'],$row['name_cat'] . '</option>'."\n";
                                } 
                                echo "</select><br />";
		$_POST['categoriesID'];

	?>
<form method="post" action="">
<b>Add new:</b>
<input type="text" name="newSubCat">
<input type="submit" name="addSubCat" value="Add">
</form>

 

Table is populated only with $newSub but not with $id_main...

Link to comment
https://forums.phpfreaks.com/topic/224842-postget-select-etc/
Share on other sites

You're using the POST method in your form, but attempting to use the value of a $_GET variable. Change $_GET['categoriesID'] to $_POST['categoriesID'] and the value should be there.

 

As an aside, it's always a good idea to validate and sanitize form data. First to make sure all the data you need is actually there, and second to prevent malicious 'users' from running SQL injection and other attacks.

Link to comment
https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161357
Share on other sites

Ok, I've managed all the issues you posted, thank you. But now form wont reload and values aren't stored.

 

New code:

if (isset($_POST['DodajPodkategorijo']))
{
$id_glavne = $_GET['kategorije'];
$DB-> Query('INSERT INTO podkategorije(id_glavne_kategorije,ime_podkategorije) VALUES ("'.$id_glavne.'","'.$novaPodkategorija.'")');
}
?>
<br />
	<?php
	$result = mysql_query("SELECT id,ime_kategorije FROM kategorije") 
                                or die(mysql_error());   
		echo "izberi glavno kategorijo:";
		echo "<select name='kategorije'>";
             //prebere vse kategorije iz tabele 'kategorije'
             while($row = mysql_fetch_array( $result )) {
             // in jih izpiše v dropdownu
		 echo '<form method="post" action="">';
		 echo '<option value="'.$row['id'].'">';
             echo $row['id'],$row['ime_kategorije'] . '</option>'."\n";
                                } 
                                echo "</select><br />";
		$_POST['kategorije'];
	?>

<b>Dodaj podkategorijo:</b>
<input type="text" name="novaPodkategorija">
<input type="submit" name="DodajPodkategorijo" value="Dodaj podkategorijo">
</form>

 

Link to comment
https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161445
Share on other sites

$novaPodkategorija is undefined in the code, unless register_globals = On (which it definitely should not). You need to assign the value of $_POST['novaPodkategorija'] to it as below, assuming it's expected to be string type data.

$novaPodkategorija = mysql_real_escape_string($_POST['novaPodkategorija']);

Link to comment
https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161512
Share on other sites

$novaPodkategorija is undefined in the code, unless register_globals = On (which it definitely should not). You need to assign the value of $_POST['novaPodkategorija'] to it as below, assuming it's expected to be string type data.

$novaPodkategorija = mysql_real_escape_string($_POST['novaPodkategorija']);

 

$novaPodkategorija is saved into "ime_podkategorije" (name of sub category) so I think this isn't an issue, I can't get ID of selected item in dropdown...

Link to comment
https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161770
Share on other sites

$novaPodkategorija is undefined in the code, unless register_globals = On (which it definitely should not). You need to assign the value of $_POST['novaPodkategorija'] to it as below, assuming it's expected to be string type data.

$novaPodkategorija = mysql_real_escape_string($_POST['novaPodkategorija']);

 

$novaPodkategorija is saved into "ime_podkategorije" (name of sub category) so I think this isn't an issue, I can't get ID of selected item in dropdown...

 

That's not in the code you provided.

Link to comment
https://forums.phpfreaks.com/topic/224842-postget-select-etc/#findComment-1161936
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.