-1

Hi I work like loop through the selection option values with PHP instead of duplicating it, from value 500 to 510. Can you kindly show me how to do it?

<body>
    <div class="form-group">
        <br> <label>Job Description</label>: <b><?php echo $row['job_desc']; ?><br></b>
        <select class="form-control" name="job_code">
            <option value="500">System Analysis</option>
            <option value="501">Programmer</option>
            <option value="502">Database Designer</option>
            <option value="503">Electrical Engineer</option>
            <option value="504">Mechanical Engineer</option>
            <option value="505">Civil Engineer</option>
            <option value="506">Clerical Support</option>
            <option value="507">DSS Analyst</option>
            <option value="508">Application Designer</option>
            <option value="509">Bio Technician</option>
            <option value="510">General Support</option>
        </select>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary" name="update" value="Update Data">Save</button>
    </div>

</body>

Employee BDJob DB

<?php
$connection = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($connection, 'amaz');

if (isset($_POST['update'])) {
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $sex = $_POST['sex'];
    $emp_salary = $_POST['emp_salary'];
    $dept_name = $_POST['dept_name'];
    $job_code = $_POST['job_code'];



    $query = "UPDATE employee 
SET employee.first_name='$_POST[first_name]',employee.last_name='$_POST[last_name]',employee.sex='$_POST[sex]',employee.emp_salary='$_POST[emp_salary]',employee.dept_id='$_POST[dept_name]',employee.job_code='$_POST[job_code]'
WHERE employee.emp_num='$_POST[id]'";
    $query_run = mysqli_query($connection, $query);
}
?>
droopsnoot
  • 931
  • 1
  • 7
  • 11
Samuel Ng
  • 1
  • 3
  • Are the job codes in a database table? If they are, write a query to retrieve the job code id and description and loop through the results to display them. – droopsnoot Oct 11 '22 at 11:24
  • 3
    You need to look at prepared statements instead of concatenating user-supplied variables into your query like that. What happens if the last name is "O'Hara" - your code will stop working. Prepared statements will fix that, along with some other stuff. – droopsnoot Oct 11 '22 at 11:25
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Oct 11 '22 at 12:49
  • Do you have any **specific** question about all this code? What **exactly** is not working? What have you tried to resolve the problem? – Nico Haase Oct 12 '22 at 09:44

1 Answers1

-1

You make a SELECT query that looks at the jobs table:

$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'amaz';

$mysql = new mysqli($server, $user, $pass, $db);

if ($mysql->connect_error !== null) {
    printf("Connect failed: %s\n", $mysql->connect_error);
    exit;
}

$select = $mysql->query("SELECT * FROM jobs WHERE job_code > 409 and job_code < 511");

$jobs = [];
while ($row = $select->fetch_array(MYSQLI_ASSOC)) {
    $jobs[] = $row;
}

?>
<select class="form-control" name="job_code">
<?php foreach ($jobs as $job): ?>
    <option value="<?=$job['job_code']?>"><?=$job['job_desc']?></option>
<?php endforeach; ?>
</select>

This will output:

<select class="form-control" name="job_code">
    <option value="500">System Analysis</option>
    <option value="501">Programmer</option>
    <option value="502">Database Designer</option>
    ...etc
</select>
Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22