gpt4 book ai didi

php - 如何将每个数字在不同字段中的电话号码插入数据库?

转载 作者:行者123 更新时间:2023-11-30 22:03:28 25 4
gpt4 key购买 nike

我做了这个,但是我怎样才能用 PHP 将这个电话号码插入到 MySQL 数据库中呢?

Screenshot of phone number input with each number in a separate box

如何制作一个带有电话号码分隔符的输入元素,并将它们插入 MySQL 数据库?

这是 HTML 代码:

<!DOCTYPE html>
<html>
<style type="text/css">
table,td{
border-collapse: collapse;
padding:0;
border:2px solid black;
}
td input{
width:20px;
}

</style>

<script type="text/javascript">
var currentDigit = 1;
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}

function moveToNextInputDigit(e){
currentDigit = parseInt(e.id.replace("digit",""));
currentDigit++;
if(e.value.length == 1 && currentDigit<=10 && document.getElementById("digit"+currentDigit).value.length!=1)
{
document.getElementById("digit"+currentDigit).focus();
}
}

function getPhoneNumber()
{
var phoneNumber = "";
for(var i=1;i<=10;i++)
{
phoneNumber += document.getElementById("digit"+i).value;
}

alert("Phone Number: "+ phoneNumber);
}

</script>

<body>

<table>
<tr>
<td><input id="digit1" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit2" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit3" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit4" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit5" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit6" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit7" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit8" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit9" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit10" type="text"maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
</tr>
</table>

<input type = "button" value="Get Phone Number" onClick="getPhoneNumber()">
</body>
</html>

最佳答案

我在下面的函数中添加了一行以包含 jQuery $.post()。您会找到有关 .post here 的更多信息.这会将电话号码发送到 insertphonenumber.php 以将其添加到数据库中。

您需要在页面头部包含指向 jquery 的链接

<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js">
</script>

我还编辑了答案以包含一种用 . 分隔每个数字的方法。使用 str_split()implode() php 函数

您可以返回数据,例如 html 警报,以确认此电话号码已添加到数据库中,如果您愿意,我可以添加它,但我在这里保持简单。

function getPhoneNumber()
{
var phoneNumber = "";
for(var i=1;i<=10;i++)
{
phoneNumber += document.getElementById("digit"+i).value;
}

// alert("Phone Number: "+ phoneNumber);
// Add in this line to send phoneNumber to insertphonenumber.php via ajax
$.post("insertphonenumber.php", { Number: phoneNumber});
}

PHP 页面:

<?php //insertphonenumber.php

$username = "root";
$password = "yourDBpassword";
$host = "localhost";
$dbname = "yourDBname";

$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');

$phoneNumber = $_POST['Number'];
// This will need to be sanitised from SQL injection or XSS attacks.
// have a look at using htmlentities();

// This next line puts a fullstop between each
// str_split() turns the string into an array of individual characters
// Implode takes the array and puts a . between each item.
$phoneNumberSplit = implode('.',str_split($phoneNumber));

// $query = " INSERT INTO yourTable(columnName) VALUES( :number ) ";
$query = " INSERT INTO tablename(columnName) VALUES( :number ) ";
$query_params = array('number' => $phoneNumberSplit);

try
{
// Prepare and execute the query to insert the phoneNumber into the DB
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
// On a production website, you should not output $ex->getMessage().
// It could provide an attacker with sensitive information about your code.
die("Failed to run query: " . $ex->getMessage());
}

关于php - 如何将每个数字在不同字段中的电话号码插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42573894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com