dsgd4654674 2015-06-04 17:55 采纳率: 0%
浏览 15

使用从php / database返回的数据来创建新表单

I did some research already but can't find a good solution to this. I am sure it's simple, but I could use some help.

I am using HTML -> Javascript -> PHP to get info back from a database. My goal is to have the data return, but have it add check boxes at the end of each row where if the person check it, it will add them to another table.

In my example, it will return a list of cards and if the person uses the check box it will add each card they checked to a "have" list. For my code provided below, it's a "display all" so I didn't use any javascript. I put it straight from html to php. I figure if I can get the most simple example working, adding the js to my other search display will be easy.

HTML

<fieldset>
<form action="display_all.php" method="post">
Order by: <select name="order_all" id="order_all">
  <option value="parallel">Parallel</option>
  <option value="faction">Faction</option>        
</select>   
<input type="submit" value="display all cards">
</form>
</fieldset>

PHP (to save space I cut out some of the standard code)

$order_all = $_POST['order_all'];

// Create SQL statement
$query = "SELECT * FROM cards ORDER BY $order_all ASC";
// Execute SQL statement
if (!($result = @ mysql_query ($query, $connection)))
  showerror();
// Display results
while ($row = @ mysql_fetch_array($result)) {

  echo "<tr><td>{$row["parallel"]}</td>
<td>{$row["faction"]}</td>
<td>{$row["in_set"]}</td>
<td>{$row["card_name"]}</td>
<td>{$row["color"]}</td>
<td>{$row["number_in_set"]}</td>
<td>{$row["rarity"]}</td>
<td>{$row["sold_out"]}</td>
<td>{$row["series"]}</td>
</tr>";
}

I tried adding different things to the while { } at the end of the PHP file but I don't really know the proper way to do that. Based on what I saw around, people suggest doing this in javascript and creating a function for it. Start with my ajax call and callback? I planned on linking my checkboxes to the ID of each card (which is stored in the database, but not printed) I figure they will just be linked to query insert commands. Like if check [ insert command ] and form submit? idk looking for some suggestions, I am still new to using databases in this sort of way.

FOUND A SOLUTION TO MY PROBLEM, well sorta. It was a bunch of little things. I haven't added functionality, but I at least got the checkbox to show up.

while ($row = @ mysql_fetch_array($result)) {
echo"
<tr>
<td>{$row["parallel"]}</td>
<td>{$row["faction"]}</td>
<td>{$row["in_set"]}</td>
<td>{$row["card_name"]}</td>
<td>{$row["color"]}</td>
<td>{$row["number_in_set"]}</td>
<td>{$row["rarity"]}</td>
<td>{$row["sold_out"]}</td>
<td>{$row["series"]}</td>
";
echo'<td><input type="checkbox" value="submit" id="{$row["id"]}/></td>';
echo"</tr>";
}
  • 写回答

1条回答 默认 最新

  • dtzk85937 2015-06-04 20:50
    关注
    1. Your code is paradise for SQL injection! You have to check user input, at least escaping with mysql_real_escape_string or
    2. Switch to newer, better and safer mysqli (or PDO) interface and use prepared statements. I recommend mysqli anyway. Note: in this particular case is best to use switch with options.

    To question: why don't you get checkbox value from post and then take actions, but I'm not sure what are you asking.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀