Ik heb het volgende script:


<html>
<head>
<script language="javascript">
fields = 10;
function addInput() {
if (fields != 1000) {
document.getElementById('text').innerHTML += "<tr><td>&nbsp;002:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr><br><tr><td>&nbsp;002:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr><br><tr><td>&nbsp;002:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr><br><tr><td>&nbsp;002:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr><br><tr><td>&nbsp;002:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr><br>";
fields += 5;
} else {
document.getElementById('text').innerHTML += "<br />Het maximum van 1.000 is bereikt.";
document.form.add.disabled=true;
}
}
</script>
<title></title>
</head>
<body>
<form name="form">
Titel: <input type='text' value='' size="75"/><br>
<table><tr><td>Taal 1:</td><td>Taal 2:</td></tr>
       <tr><td><input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
</table>
Inhoud:<br>
<table><tr><td>001:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
<tr><td>002:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
<tr><td>003:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
<tr><td>004:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
<tr><td>005:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
<tr><td>006:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
<tr><td>007:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
<tr><td>008:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
<tr><td>009:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
<tr><td>010:</td><td> <input type='text' value='' /></td><td><input type='text' value='' /></td></tr>
</table>
<div id="text">
<table>

</table>
</div>
<input type="button" onclick="addInput()" name="add" value="Meer velden toevoegen" /></table></form></body>
</html>


Nu wil ik, dat er inplaats van alleen maar 002 er 011, 012 etc. komt te staan en dat deze ook als name komen te staan bij het veld. Hoe doe ik dit?
De regel eindigt met een puntkomma. Dus is ie afgesloten en er mist nogal wat. Er kan niet middenin een foreach() zitten.
Ik kom er niet uit... Hoe zou het script volgens jou moeten zijn?
'Ik kom er niet uit' hebben we niks aan. Toon relevante code en foutmeldingen.
Volgens mij zou het zoiets moeten zijn
<?php
include 'bgup.php';
error_reporting(E_ALL);

if(isset($_POST['add'])){
$sql = "INSERT INTO woorden_lijsten(titel,inhoud_1,inhoud_2) VALUES ";

foreach($_POST['titel'] as $key => $value) {
if(trim($value) != '') {
$sql .= "('" . mysql_real_escape_string($value) . "',
'" . mysql_real_escape_string($_POST['links'][$key]) . "',
'" . mysql_real_escape_string($_POST['rechts'][$key]) . "')";
}
}
$res = mysql_query($sql);
if($res){
echo 'Insert done';
} else {
print(mysql_error());
}
}
?>

Daarbij let op dat MySQL 'depraceted' is wat inhoud dat deze functie in de toekomst zal verdwijnen en niet meer wordt aangeraden om te gebruiken.

Met isset($_POST['add']) controleer je of een formulier is gepost? Gebruik hier ($_SERVER['REQUEST_METHOD'] == 'POST') voor

[edit]typo[/edit]
Ger van Steenderen op 13/12/2013 18:23:06


<?php
$values = array();
$sql = "INSERT INTO items (titel, links, rechts) VALUES ";
foreach ($_POST['titel'] as $key => $value) {
	if (trim($value) != '') {
		$values[] = "('" . $db->escape($value) . "','" .
			$db->escape($_POST['links'][$key]) . "','" . $db->escape($_POST['rechts'][$key]) ."')";
	}
}
$sql .= implode(',',$values);
?>


Met deze code wordt een query opgebouwd waarmee je in één keer meerdere rijen in een tabel kan invoegen. Een voorbeeld om op weg te helpen, en die query moet natuurlijk nog wel uitgevoerd worden.


Ik krijg deze error:

Warning: Invalid argument supplied for foreach() in /home/kennemer/public_html/login_succes_nieuw/lijsten_aanmaken.php on line 7
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Bij dit script:


<?php
include 'bgup.php';
if(isset($_POST['sturen'])){
$values = array();
    $sql = "INSERT INTO woorden_lijsten(titel,inhoud_1,inhoud_2) VALUES ";
    foreach($_POST['titel'] as $key => $value) {
        if(trim($value) != '') {
            $sql .= "('" . mysql_real_escape_string($value) . "',
            '" . mysql_real_escape_string($_POST['links'][$key]) . "',
            '" . mysql_real_escape_string($_POST['rechts'][$key]) . "')";
        }
    }
    $res = mysql_query($sql);
    if($res){
        echo 'Insert done';
    } else {
        print(mysql_error());
    }
}
?>
<html>
<head>
<style>
.stuur {
        width:150px;
        height:35px;
        margin:0px 20px 0px 0px;
  -moz-border-radius: 4px 4px 4px 4px;
   -webkit-border-radius: 4px 4px 4px 4px;
   border-radius: 4px 4px 4px 4px;
        background-color:#000000;
        color:#FFFFFF;
}
</style>
<script language="javascript">
function addInput(x)
{
    if(fields < fieldmax)
    {
        fields += 1;
        if(fields < 10)            nummer = '00'+fields;
        else if(fields < 100)    nummer = '0'+fields;
        else                     nummer = fields;
        document.getElementById('row').innerHTML += '<tr><td><font color=white>'+nummer+':</font></td><td><input type="text" name="links['+nummer+']" value="" /></td><td><input type="text" name="rechts['+nummer+']" value="" /></td></tr><br>';
    } 
    else
    {
        document.form.add.disabled=true;
        document.getElementById('max').innerHTML = 'Het maximum van '+fieldmax+' is bereikt.';
    }
    if(x > 1) addInput(x-1);
}

fields        = 0;
fieldmax    = 1000;
addInput(10);
</script>
<title></title>
</head>
<body>
<form name="form" method="post" action="">
<font color='white'>Titel:</font> <br><input type='text' name='titel' value='' size="75"/><br>
<table><tr><td><font color='white'>Taal 1:</font></td><td><font color='white'>Taal 2:</font></td></tr>
       <tr><td><input type='text' name='t1' value='' /></td><td><input type='text' name='t2' value='' /></td></tr>
       <tr><td><font color='white'>Vak:</font> </td><td><font color='white'>Leerjaar:</font> </td></tr>
       <tr><td><select name="vak">
       <option DISABLED SELECTED>Kies een vak</option>
       <option>Nederlands</option>
<option>Engels</option>
<option>Frans</option>
<option>Duits</option>
<option>Geschiedenis</option>
<option>Aardrijkskunde</option>
<option>Wiskunde</option>
<option>Techniek</option>
<option>Biologie</option>
<option>Maatschappelijke Vorming</option>
<option>Natuurkunde</option>
<option>Muziek</option>
<option>Economie</option>
<option>Scheikunde</option>
</select></td><td><select name="jaar">
<option DISABLED SELECTED>Kies een leerjaar</option>
<option>Brugklas</option>
<option>Havo 2</option>
<option>VWO 2</option>
<option>Havo 3</option>
<option>VWO 3</option>
<option>Havo 4</option>
<option>VWO 4</option>
<option>Havo 5</option>
<option>VWO 5</option>
<option>VWO 6</option>
</select></td></tr>
</table>
<form method="post" name="form">
    <table>
        <div id="row0">
        </div>
    </table>
    <div id="max">
    </div>
    <button type="button" name="add" onclick="addInput(5)" class="stuur">Meer velden</button>
    <input type="submit" class="stuur" name='sturen' value='Versturen'><br />
</form>

<script language="javascript">
function addInput(x)
{
    if(fields < fieldmax)
    {
        getid    = 'row'+fields;
        fields += 1;
        putid    = 'row'+fields;
        if(fields < 10)            nummer = '00'+fields;
        else if(fields < 100)    nummer = '0'+fields;
        else                     nummer = fields;
        document.getElementById(getid).innerHTML = '<tr><td><font color=white>&nbsp;'+nummer+':</font></td><td><input type="text" name="links['+nummer+']" value="" /></td><td><input type="text" name="rechts['+nummer+']" value="" /></td></tr></div><div id="'+putid+'">';
        if(fields == fieldmax)
        {
            document.form.add.disabled=true;
            document.getElementById('max').innerHTML = 'Het maximum van '+fieldmax+' is bereikt.';
        }
    }
    if(x > 1) addInput(x-1);
}

fields        = 0;
fieldmax    = 1000;
addInput(10);
</script>
</html>
<?php
include 'bgdown.php';
?>


Wat gaat er mis?
Er gaat een hele hoop mis.
Die foutmelding krijg je omdat $_POST['titel'] geen array is. Foreach gebruik je voor arrays.
Wat doet values() ?
if(isset($_POST['sturen'])){ is de foute manier om te kijken of iets gepost is.
Gebruik hier, zoals ik al eerder zei, if($_SERVER['REQUEST_METHOD'] == 'POST'){ voor.

Reageren