0
votes

Please help me create a foreach loop that takes the content of an array, which includes each name attribute from an html form, and caches it as a php variable which can be used to submit the data from the form into a mySQL database.

I have included an instance (of many failed attempts) of my foreach in the php code below. The form works fine if my foreach loop is excluded and the commented out variable assignment in php is uncommented.

Here is the HTML form, first:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Fixit-log</title>
</head>
<body>
    <form action="form_script.php" method="POST">
        <section id="identification"
            <p>Case: <input type="text" name="case_id" size="7"/></p>
            <p>Date: <input type="text" name="day" size="15" /</p>
            <p>Owner First Name: <input type="text" name="owner_first_name" size="15" /> </p>
            <p>Owner Last Name: <input type="text" name="owner_last_name" size="20" /</p>
            <p>Phone: <input type="text" name="phone" size= "10"/></p>
            <p>Email: <input type="email" name="email" size= "30"/><p>
            <p>Address: <input type="text" name="address" size= "50"/</p>
            <p>City: <input type="text" name="city" size="30"/</p>
            <p>State: <input type="text" name= "state" size="2"</p>
            <p>ZIP: <input type="text" name="zip" size="5"</p>
        </section>

        <section id="hardware">
            <p>OS Make: <input type="radio" name="os_make" value="windows"/>Windows
                <input type="radio" name="os_make" value="mac"/> Mac
                <input type="radio" name="os_make" value="linux"/> Linux
                <input type="radio" name="os_make" value="none"/>None</p>
            <p>OS Type: <input type="text" name="os_type" size="15"/</p>
            <p>OS Architecture <input type="radio" name="os_architecture" value="32"/> 32
                <input type="radio" name="os_architecture" value="64"/>64
                <input type="radio" name="os_architecture" value="other"/>Other</p>
            <p>S/N: <input type="text" name="sn" size="20"</p>
            <p>RAM: <input type="text" name="ram_amount" size="10"</p>
            <p>CPU Make: <input type="radio" name="cpu_make" value="intel"/>Intel
                <input type="radio" name="cpu_make" value="amd"/>AMD</p>
                <input type="radio" name="cpu_make" value="other">Other</p> 
            <p>CPU Model: <input type="text" name="cpu_model" size="0"/></p>
            <p>CPU Architecture: <input type="radio" name="cpu_architecture" value="32"/>32
                <input type="radio" name="cpu_architecture" value="64"/>64
                <input type="radio" name="cpu_architecture" value="other"/>Other</p>
            <p>Other equipment: <input type="text" name="equipment_other" size="50"</p>
        </section>

        <section id="history">
            <p>C/O: <input type="text" name="co" size="255"</p>
            <p>Began: <input type="text" name="began" size="30"</p>
            <p>Previous Work: <input type="radio" name="previous_work" value="yes"/>Yes
                <input type="radio" name="previous_work" value="no"/> No
                <input type="radio" name="previous_work" value="unknown"/> Unknown</p>
            <p>Who?<input type="text" name="previous_who" size="20"/></p>
            <p>When?<input type="text" name="previous_when" size="20"/></p>
            <p>Comments: <input type="text" name="comments_initial" size="255"/></p>
        </section>

        <section id="diagnostic">
            <p>Boot Time (sec): <input type="text" name="boot_time_initial" size="4"/</p>
            <p>Memory: <input type="text" name="memory_use_initial" size="3" /</p>
            <p>CPU: <input type="text" name="cpu_use_initial" size="3"/</p>
            <p>Evaluation <input type="text" name="evaluation" size="254"</p>
        </section>

        <section id="plan">
            <p>Actions: <input type="text" name="actions" size="254"/</p>
            <p>Overview: <input type="text" name="overview" size="254"/</p>
            <p>Summary: <input type="text" name="summary" size="254"/</p>
            <p>Recommendations: <input type="text" name="recommendations" size="254"/</p>
        </section>

        <input type="submit" name="submit" value="Submit">
        <input type="reset" name="reset" value="Reset">
    </form>
</body>
</html>

Here is the PHP script:

<?php
define('DB_NAME', 'x');
define('DB_USER', 'x');
define('DB_PASSWORD', 'x');
define('DB_HOST', 'x');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

/*echo 'connected succesfully';*/

$formItems = array('case_id','day','owner_first_name','owner_last_name','phone','email','address','city','state','zip','os_make','os_type','os_architecture','sn','ram_amount','cpu_make','cpu_model','cpu_architecture','equipment_other','co','began','previous_work','previous_who','previous_when','comments_initial','boot_time_initial','memory_use_initial','evaluation','overview','actions','summary','recommendations');

foreach ($formItems as $formItem){
    print '$' . $formItem . '=' . '$_POST' . '[' . '\'' . $formItem . '\'' . '];' . ' ';
}
//Loop replaces list below

/*$case_id=$_POST['case_id'];
$day=$_POST['day'];
$owner_first_name=$_POST['owner_first_name'];
$owner_last_name=$_POST['owner_last_name'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$address=$_POST['address'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$os_make=$_POST['os_make'];
$os_type=$_POST['os_type'];
$os_architecture=$_POST['os_architecture'];
$sn=$_POST['sn'];
$ram_amount=$_POST['ram_amount'];
$cpu_make=$_POST['cpu_make'];
$cpu_model=$_POST['cpu_model'];
$cpu_architecture=$_POST['cpu_architecture'];
$equipment_other=$_POST['equipment_other'];
$co=$_POST['co'];
$began=$_POST['began'];
$previous_work=$_POST['previous_work'];
$previous_who=$_POST['previous_who'];
$previous_when=$_POST['previous_when'];
$comments_initial=$_POST['comments_initial'];
$boot_time_initial=$_POST['boot_time_initial'];
$memory_use_initial=$_POST['memory_use_initial'];
$cpu_use_initial=$_POST['cpu_use_initial'];
$evaluation=$_POST['evaluation'];
$overview=$_POST['overview'];
$actions=$_POST['actions'];
$summary=$_POST['summary'];
$recommendations=$_POST['recommendations'];
*/

$sql = "INSERT INTO jobs (case_id, day, owner_first_name, owner_last_name, phone, address, city, state, zip, email, os_make, os_type, os_architecture, sn, ram_amount, cpu_make, cpu_model, cpu_architecture, equipment_other, co, began, previous_work, previous_who, previous_when, comments_initial, boot_time_initial, memory_use_initial, cpu_use_initial, evaluation, overview, actions, summary, recommendations) " . " VALUES ('$case_id', '$day', '$owner_first_name', '$owner_last_name', '$phone', '$address', '$city', '$state', '$zip', '$email', '$os_make', '$os_type', '$os_architecture', '$sn', '$ram_amount', '$cpu_make', '$cpu_model', '$cpu_architecture', '$equipment_other', '$co', '$began', '$previous_work', '$previous_who', '$previous_when', '$comments_initial', '$boot_time_initial', '$memory_use_initial', '$cpu_use_initial', '$evaluation', '$overview', '$actions', '$summary', '$recommendations')";

if(!mysql_query($sql)) {
    die('Error: ' . mysql_error());
}

    mysql_close();  
?>

And, here is the error the error I receive:

$case_id=$_POST['case_id']; $day=$_POST['day']; $owner_first_name=$_POST['owner_first_name']; $owner_last_name=$_POST['owner_last_name']; $phone=$_POST['phone']; $email=$_POST['email']; $address=$_POST['address']; $city=$_POST['city']; $state=$_POST['state']; $zip=$_POST['zip']; $os_make=$_POST['os_make']; $os_type=$_POST['os_type']; $os_architecture=$_POST['os_architecture']; $sn=$_POST['sn']; $ram_amount=$_POST['ram_amount']; $cpu_make=$_POST['cpu_make']; $cpu_model=$_POST['cpu_model']; $cpu_architecture=$_POST['cpu_architecture']; $equipment_other=$_POST['equipment_other']; $co=$_POST['co']; $began=$_POST['began']; $previous_work=$_POST['previous_work']; $previous_who=$_POST['previous_who']; $previous_when=$_POST['previous_when']; $comments_initial=$_POST['comments_initial']; $boot_time_initial=$_POST['boot_time_initial']; $memory_use_initial=$_POST['memory_use_initial']; $evaluation=$_POST['evaluation']; $overview=$_POST['overview']; $actions=$_POST['actions']; $summary=$_POST['summary']; $recommendations=$_POST['recommendations']; Notice: Undefined variable: case_id in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: day in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: owner_first_name in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: owner_last_name in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: phone in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: address in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: city in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: state in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: zip in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: email in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: os_make in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: os_type in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: os_architecture in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: sn in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: ram_amount in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: cpu_make in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: cpu_model in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: cpu_architecture in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: equipment_other in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: co in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: began in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: previous_work in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: previous_who in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: previous_when in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: comments_initial in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: boot_time_initial in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: memory_use_initial in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: cpu_use_initial in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: evaluation in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: overview in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: actions in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: summary in /opt/lampp/htdocs/fixit-log/form_script.php on line 63

Notice: Undefined variable: recommendations in /opt/lampp/htdocs/fixit-log/form_script.php on line 63 Error: Duplicate entry '0' for key 'case_id'

1
your main issue is this Error: Duplicate entry '0' for key 'case_id'. Your SQL server has unique index on case_id column, which you are trying to add a duplicate key for.Dimi
This code is subject to SQL Injection vulnerability, because it includes potentially unsafe values within the text of a SQL statement. Potentially unsafe values must be properly escaped. The mysql interface is deprecated, new development should use either mysqli or PDO.spencer7593
Thank you Dimi and spencer7593 for your responses. When I exclude the foreach loop there is no errors at all. The data is submitted. However, when the loop is present, and making sure to use a unique case_id, I reach that error. spencer7593 - thank you, I will look into those interfaces.squadguy

1 Answers

1
votes

It looks like you are trying to create variables with names taken from your $formItems array. I think you could use variable variables for this. Doing it the way you show in your question:

foreach ($formItems as $formItem){
    print '$' . $formItem . '=' . '$_POST' . '[' . '\'' . $formItem . '\'' . '];' . ' ';
}

will really just print out what you are trying to accomplish instead of actually doing it. Instead you can use this to get the variables you want:

foreach ($formItems as $formItem) {
   $$formItem = mysql_escape_string($_POST[$formItem]);
}

On a side note, you should escape these values before attempting to insert them into your database. I added escaping the values using the mysql function, because you are using those in your code. But these functions are deprecated and you should look into changing your code to use mysqli or PDO instead.