从php中的表更新数据库

时间:2016-09-17 03:38:35

标签: php sql database html-table

我可以在我的网页上连接和查看数据库。我创建了输入字段,以便我可以编辑数据库。但是,单击编辑按钮时,我无法更改数据库。我希望我正确地格式化了这个问题。我很抱歉我是编码的新手。

    <?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

    ?>

2 个答案:

答案 0 :(得分:0)

获得POST数据后,您应该使用[]。尝试改变它:

$_POST(name)

$_POST['name']

这将是:

$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']}'"; 

答案 1 :(得分:0)

将代码更改为上面列出的答案可以解决问题。但我也不得不改变

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

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