doutui7955 2015-10-27 21:20
浏览 13

在mysql中从5个表中插入2个表

How to insert part of column in MySQL using php ? For example,my column table in sql are : code,Title,Cr,prerequisite,Addit,Grade I used table to fetch the data in php , but Addit and Grade is empty . I want user to insert them the other column is fixed with specific data .

How to insert in just in Addit and Grade ?

The problem i face in this code is that the insert data is coming alone in row not in Addit and Grade with the names of course .

I want from student to define the grade of course name university studies weather A,B,C,D. And ,to define weather students complete or not complete this course.

enter code here
 <html>
 <head>

</head>
  <body>
  <form action="insert.php" name="frmAdd" method="post">

<table border='1' cellpadding='5' cellspacing='1' id='mytable'>
   <tr align='center'>
          <th>code</th>
     <th>Title</th>
     <th>Cr</th>
          <th>prerequisite</th>

                <th>Addi</th>
 <th>grade</th>
   </tr>
   <?php
  error_reporting(0);
      $sql="select code,Title,Cr,prerequisite,Addit,Grade from study_p";
     $result=mysql_query($sql);
       while ($row = mysql_fetch_array($result))`


 $code  = $row['code'];
 $Title = $row['Title'];
      $cr = $row['Cr'];
    $pre = $row['prerequisite'];
    $addit= $row['Addit'];
   $grade = $row['Grade'];


    echo "<tr  class='edit_tr' id='$id'>
      <td class='edit_td'>
        <span class='text' id='one_$ssn' >$code </span>
        <input type='text' class='editbox_search' id='one_input_$ssn'
 value='$code ' >
      </td>

      <td class='edit_td'>
        <span class='text' id='two_$ssn' >$Title</span>
           <input type='text' class='editbox_search' id='two_input_$ssn'      

    value='$Title' >
      </td>

      <td class='edit_td'>
        <span class='text' id='three_$ssn' >$cr</span>
        <input type='text' class='editbox_search' id='three_input_$ssn'
    value='$cr' >
      </td>

      <td class='edit_td'>
        <span class='text' id='three_$ssn' >$pre</span>
        <input type='text' class='editbox_search' id='three_input_$ssn'



      value='$pre' >
      </td>
      <td class='edit_td'>
        <span class='text' id='three_$ssn' >$addit</span>


        <select name='Addit' id='addit'>
         <option value='A'>Completed</option>
        <option value='B'>Not complate</option>

</select>

</td> 
      <td class='edit_td'>
        <span class='text' id='three_$ssn' >$grade</span>

                                <select name='Grade' id='grade'>
        <option value='A'>A</option>
        <option value='B'>B</option>
        <option value='C'>C</option>
        <option value='D'>D</option>
        <option value='F'>F</option>
</select>

</td> 

</tr>";  
 }             
?>

<p><input type="submit" value="Add Link"></p>
</tr>
  </table>
 </div>



insert.php



enter code here
<?php 
$usr = "";
$pwd = "";
$db = "adv";
$host = "localhost";

 $cid = mysql_connect($host,$usr,$pwd);

  if (!$cid) { echo("ERROR: " . mysql_error() . "
"); }
?>

<?php 

if ($_SERVER['REQUEST_METHOD'] == "POST") { 



$addit= $_POST['Addit'];
$grade = $_POST['Grade'];


$sql = " INSERT INTO study_p";
$sql = $sql . " (code,Title,Cr,prerequisite,Addit,Grade) VALUES ";
$sql = $sql . " ('','','','','$addit', '$grade') ";
    $result = mysql_db_query($db,"$sql",$cid);

 if (!$result) { 
 echo("ERROR: " . mysql_error() . "
$sql
");   }

echo ("New Link Added
");

 } 


  mysql_close($cid); 
   ?>

sql

enter code here

  CREATE TABLE IF NOT EXISTS `study_p` (
   `Study_pID` int(25) NOT NULL,
  `code` varchar(25) DEFAULT NULL,
  `Title` varchar(100) DEFAULT NULL,
  `Cr` int(200) DEFAULT NULL,
   `prerequisite` varchar(25) DEFAULT NULL,
   `Addit` varchar(25) DEFAULT NULL,
  `Grade` varchar(100) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;



    insert into studyplancourses values(2, 'UNS 100', 'University Study
    Skills', 3, 'No Prerequisite', NULL, ''),
   insert into studyplancourses values(3, 'ENG 100', 'English I', 3, 'ELI 
     Exit 
   Test or TOEFL 50', NULL, ''),
  • 写回答

1条回答 默认 最新

  • doumie7914 2015-10-27 22:51
    关注

    You are using the wrong sql command. INSERT command will insert a new record into a table. What you want is to UPDATE an existing record. You need to use the UPDATE command to accomplish this.

    You need to use the WHERE clause in the update statement to tell mysql which row(s) you would like to update.

    Sample sql code:

    UPDATE study_p --which table to update
    SET addit='...', grade='...' --which fields to update
    WHERE code='...' --which record to update
    

    Pls note, that I assumed that the code field uniquely identifies a record. If this is not the case, then you need to use a different where criteria based on your data.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度