简体   繁体   中英

Updating database from table in php

I am able to connect and view database on my webpage. I have created input fields so that I can edit the database. However, I can't get the changes to update to the database when I click the edit button. I hope I formatted the question correctly. I apologize I am new to coding.

    <?php 

    $con = mysqli_connect('localhost','******','******');

if(!$con){
    echo 'Not Connected to Server';
}

if(!mysqli_select_db($con,'*********')){
    echo 'Databaase Not Selected';
    }

if(isset($_POST['submit']) &&$_POST['submit']=='edit'){

 $UpdateQuery = "UPDATE maininfo SET name='$_POST(name)',   phone_number='$_POST(phone_number)', address='$_POST(address)', ems_number='$_POST(ems_number)', ems_level='$_POST(ems_level)', emergency_contact_name='$_POST(emergency_contact_number)', emergency_contact_number='$_POST(emergency_contact_number)', email='$_POST(email)', hire_date='$_POST(hire_date)' WHERE id='$_Post(hidden)'"; 

mysqli_query($con, $UpdateQuery);    
}
$sql="SELECT * FROM maininfo ORDER BY name"; 

$records=mysqli_query($con, $sql);

?> 

<?php 

    while($maininfo=mysqli_fetch_assoc($records)){

        echo "<form action=employee_edit.php method=post>";

        echo "<tr>";

        echo '<td><input type=text name=id value="' . $maininfo['id']. '" /></td>';

        echo '<td><input type=text name=name value="' . $maininfo['name']. '" /></td>';

        echo '<td><input type=text name=phone_number value="' . $maininfo['phone_number']. '" /></td>';

        echo '<td><input type=text name=address value="' . $maininfo['address']. '" /></td>';

        echo '<td><input type=text name=ems_number value="' . $maininfo['ems_number']. '" />   </td>';

        echo '<td><input type=text name=ems_level value="' . $maininfo['ems_level']. '" /> </td>';

        echo '<td><input type=text name=emergency_contact_name value="' . $maininfo['emergency_contact_name']. '" /></td>';

        echo '<td><input type=text name=emergency_contact_number value="' . $maininfo['emergency_contact_number']. '" /></td>';

        echo '<td><input type=text name=email value="' . $maininfo['email'].'" /></td>';

        echo '<td><input type=text name=hire_date value="' . $maininfo['hire_date'].'" />
        </td>';

        echo '<td><input type=hidden name=hidden value="' . $maininfo['id'].'" />
        </td>';

        echo "<td>" . "<input type=submit name=edit value=edit></td>";



        echo "</form>";

    }//end while

    ?>

When you get POST data you should use [] . Try change it:

$_POST(name)

to

$_POST['name']

It would be:

$UpdateQuery = "UPDATE maininfo SET name='{$_POST['name']}',   phone_number='{$_POST['phone_number']}', address='{$_POST['address']}', ems_number='{$_POST['ems_number']}', ems_level='{$_POST['ems_level']}', emergency_contact_name='{$_POST['emergency_contact_name']}', emergency_contact_number='{$_POST['emergency_contact_number']}', email='{$_POST['email']}', hire_date='{$_POST['hire_date']}' WHERE id='{$_POST['hidden']}'"; 

Changing the code to the answers listed above corrected the problem. But I also had to change

 if(isset($_POST['submit']) &&$_POST['submit']=='edit')

to

 if(isset($_POST['edit']))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM