[英]Phonebook with multiple numbers (PHP-PDO)
I need to create a phone book with multiple phone numbers using PHP and MySQL.我需要使用 PHP 和 MySQL 创建具有多个电话号码的电话簿。
This is the phones table:这是电话表:
+----+---------+--------+
| ID | user_id | number |
+----+---------+--------+
| 1 | 1 | 111111 |
| 2 | 1 | 222222 |
| 3 | 1 | 333333 |
| 4 | 2 | 111111 |
+----+---------+--------+
And this is my code:这是我的代码:
<?php
$conn = new PDO("mysql:host=127.0.0.1;dbname=phonebook", "root", "");
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$newPhones = $_POST['phones'];
foreach ($newPhones as $i => $newPhone) {
//If the user modify a saved number, UPDATE.
//else INSERT.
}
}
$resp = $conn->query("SELECT number FROM `phones` WHERE `user_id`=1");
while ($phone = $resp->fetch(PDO::FETCH_ASSOC)) $phones[] = $phone['number'];
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<hr>
<form action="" method="POST">
<div class="phones">
<?php if (!isset($phones)) $phones[] = ''; ?>
<?php foreach ($phones as $i => $phone): ?>
<div class="phone">
<input type="text" name="phones[]" value="<?=$phone?>" /><a href="#" class="remove" style="<?php if ($i==0) echo "display:none;"; ?>">Remove</a>
</div>
<?php endforeach; ?>
</div>
<a id="add" href="#">New</a>
<input type="submit" name="submit" value="Save" />
</form>
</body>
</html>
<script type="text/javascript">
$('#add').on('click',function(e){$('.phones').after($('.phones .phone:first').clone(true,true).children('.remove').show().end().find("input:text").val("").end());e.preventDefault();});
$('.remove').on('click',function(e){$(this).parent().remove();e.preventDefault();});
</script>
My problem is when the user modifies or adds a phone numbers, how can i insert the new phone numbers entered by the user or update the phone numbers changed by the user in the HTML form?我的问题是当用户修改或添加电话号码时,我如何插入用户输入的新电话号码或更新用户在 HTML 表单中更改的电话号码?
Edit #1: I added a column to my table called "position" and I'm trying to use the position number of phone in the POST
array to identify it.编辑 #1:我在我的表中添加了一个名为“位置”的列,我正在尝试使用POST
数组中电话的位置编号来识别它。
This is my new table:这是我的新表:
+----+---------+--------+----------+
| ID | user_id | number | position |
+----+---------+--------+----------+
| 1 | 1 | 111111 | 0 |
| 2 | 1 | 222222 | 1 |
| 3 | 1 | 333333 | 2 |
| 4 | 2 | 111111 | 0 |
+----+---------+--------+----------+
And this my new PHP submit code:这是我新的 PHP 提交代码:
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$newPhones = $_POST['phones'];
foreach ($newPhones as $i => $newPhone) {
$resp = $conn->query("UPDATE `phones` SET `number`={$newPhone} WHERE `user_id`=1 AND `position`={$i} LIMIT 1");
}
}
But now, the problem is, UPDATE
only work if the row exist, but if not exist, how can i insert it?但是现在,问题是, UPDATE
仅在行存在时才有效,但如果不存在,我该如何插入它?
I tried using the value returned by PDO::rowCount
function (if it returns 0, insert it).我尝试使用PDO::rowCount
函数返回的值(如果它返回 0,则插入它)。 But if the value is not modified, PDO::rowCount
also return 0.但是如果值没有被修改, PDO::rowCount
也会返回 0。
How can i solve this problem?我怎么解决这个问题?
My suggestion is as follows (however this works on the basis that you know the User ID - your question has a hard coded UserID):我的建议如下(但是,这是在您知道用户 ID 的基础上起作用的 - 您的问题具有硬编码的用户 ID):
That way you won't need to worry about updating or keys - you simply reset that chunk of user data.这样您就无需担心更新或密钥 - 您只需重置该用户数据块。
Note that the transaction is important - you don't want to find you delete your entries and then your code fail and you lose what you had.请注意,交易很重要 - 您不想发现您删除了您的条目,然后您的代码失败并且您失去了您拥有的东西。
I hope that helps.我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.