3

I have to post a value via "form post" and insert it into table, here is my code in both files:

<html>
<body>
<table>
<form  enctype="multipart/form-data" action="<?php $_SERVER["DOCUMENT_ROOT"] ?>  /contents/ad_posting_process_4.php" method="post">
<?php $cat_no = "101010"; ?>
<input type=hidden id="category" value=" <?php echo $cat_no; ?> ">  
<tr> <td>Sub Category: </td><td>    <input type=text id="sub_category" > </td>
<tr><td></td> <td><input type="submit" name="action" value="Post"></td></tr></tr>
</form>
</body></html>

here is ad_posting_4.php

<?php session_start();
include($_SERVER["DOCUMENT_ROOT"]."/includes/conn.php");
$category = mysql_real_escape_string($_POST['category']);
$sub_category = mysql_real_escape_string($_POST['sub_category']);
echo "category=". $category; 
echo "sub_category=". $sub_category; ?>

No value sent through post.

where am I wrong?

Regards:

2
  • in ad_posting_4.php, add the line "print_r($_POST);" after the session_start(); Commented Oct 4, 2012 at 14:19
  • Typically you would use enctype="multipart/form-data" when uploading a file. It looks like from your input fields that you're passing strings. This may be causing an issue. Commented Oct 4, 2012 at 14:23

3 Answers 3

9

You need to use the name attribute:

<input type="text" name="category" />
<input type="text" name="sub_category" />
Sign up to request clarification or add additional context in comments.

1 Comment

the category input is hidden and not text
7

the input type needs to be enclosed in quotes ' and also have a name attribute, and not id.

<input type='hidden' name="category" value=" <?php echo $cat_no; ?> " />  
<tr> <td>Sub Category: </td>
<td><input type='text' name="sub_category" > </td>

Comments

0

I recently did something very similar with my own website and received help from this community. On the HTML side I created a standard form and gave each input a "name." For example let's say you are trying to capture city and state:

<html>
<body>
<form>
<tr>
<td>State: </td><td> <input type="text" style="border:1px solid #000000" name="state" /></td>
<td>City</td><td><input type="text" style="border:1px solid #000000" name="city" /></td>
</tr>
</form>
</body>
</html>

Then set up a mySQL database with a column named "state" and one named "city". Next, use PHP to insert the data from the form into your database. I am new to PHP, but from what I understand using PDOs is more secure than using the old mysql commands.

$dbtype     = "mysql";
$dbhost         = "localhost";
$dbname     = "name";
$dbuser     = "user";
$dbpass     = "pass";

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '[Insert Name of your table here]'";
$q = $conn->prepare($sql);
$q->execute();
$columns = $q->fetchAll(PDO::FETCH_COLUMN, 0);

$cols = array();
foreach ($_POST as $key=>$value)
{
    // if a field is passed in that doesn't exist in the table, remove it.  The name of the input that is removed will be echoed so you can debug.  Remove echo if you go to production.
    if (!in_array($key, $columns)) {
        unset($_POST[$key]);
echo $key;
    }
}
$cols = array_keys($_POST);
$sql = "INSERT INTO Facilities(". implode(", ", $cols) .") VALUES (:". implode(", :", $cols) .")";
$q = $conn->prepare($sql);
array_walk($_POST, "addColons");
$q->execute($_POST);

function addColons($value, &$key)
{
    $key = ":{$key}";
}

This has been working out very well for me. Note that it can only match HTML form inputs with columns of the exact same name. In my case I wanted to create over 100 inputs so this was easier. If you are dealing with 5-10 it might be easier to just insert the specific variables manually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.