hello. i've created this with help from a friend.

|uid || id ||url|

i have input fields to add the data.

<table width="100%">
   <tr>
        <td width="140" style="padding:4px;">url 1:</td>
        <td ><input type="text" class="edit bk" name="urls[]" style="width:350px"></td>
    </tr>
   <tr>
     <td width="140" style="padding:4px;">link 2:</td>
        <td ><input type="text" class="edit bk" name="urls[]" style="width:350px"></td>
    </tr>
    <tr>
        <td width="140" style="padding:4px;">link 3:</td>
        <td ><input type="text" class="edit bk" name="urls[]" style="width:350px"></td>

    <tr>
  
</table>

here is my php array

<?php

if(isset($_POST['urls']))
{
 
    $urls = array_map(array_filter(array_map($_POST['urls'], 'trim')), 'htmlspecialchars');
    //Create array to hold insert values
    $values = array();
    //Create array for results messages
    $results = array();

    //Process processed post data
    foreach($_POST['urls'] as $url)
    {
        if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $url))
        {
            $results[] = "'{$url}' is an invalid url.";
        }
        else
        {
            $values[] = "('{$row}', '{$url}')";
            $results[] = "'{$url}' was inserted nto database.";
        }
    }
    $query = "INSERT INTO " . PREFIX . "_post_links (id, url)
              VALUES" . implode(', ', $values);
    $db->query($query);

    echo implode("<br>\n", $results);
}

?>

I'd like to add another column to my table which will be called 'name'. How do I do this?

Ive tried

foreach($_POST['urls'] as $url) && ($_POST['names'] as $name )

but this returns an error.

is there a way to put two $_post into one foreach? I'd just like to add another input field into my form and have it pass to the database.

Recommended Answers

All 2 Replies

Member Avatar for diafol

Problem I see is that you're providing a success/fail msg before you try the update.

I assume you have
input1: url textbox, name textbox
input2: url textbox, name textbox
input3: url textbox, name textbox
...and so on...

So, the indexes of the arrays should match

for($x=0;$x<count($_POST['url']);$x++){
   $ok == true;
   //check validity of url and name - if either not OK, set $ok = false;
   if($ok == true){
     $entries[] = "('" . mysql_real_escape_string($_POST['url'][$x] . "','" . mysql_real_escape_string($_POST['name'][$x]) . "')";
   }
}

$valuestring = implode(",",$entries);

$query = mysql_query("INSERT INTO " . PREFIX . "_post_links (url, `name`) VALUES ($valuestring)");

NOT TESTED

//EDIT

Thought about it and this could be a lot better. Anyway, gives a vague idea.

Do you mean like the following:

foreach($_POST['urls'] as $url) {
foreach ($_POST['names'] as $name ) {

//insert code here

}}

However be sure that $_POST and $_POST are both arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.