Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在数据库中插入每个数字位于不同字段中的电话号码?_Php_Html_Mysql - Fatal编程技术网

Php 如何在数据库中插入每个数字位于不同字段中的电话号码?

Php 如何在数据库中插入每个数字位于不同字段中的电话号码?,php,html,mysql,Php,Html,Mysql,这是我做的,但是如何将这个电话号码插入PHP中的MySQL数据库呢 如何创建一个带有电话号码数字分隔符的输入元素,并将其插入MySQL数据库 以下是HTML代码: 表,td{ 边界塌陷:塌陷; 填充:0; 边框:2件纯黑; } td输入{ 宽度:20px; } var currentDigit=1; 函数isNumberKey(evt){ var charCode=(evt.which)?evt.which:event.keyCode 如果(字符码>31&(字符码57)) 返回false;

这是我做的,但是如何将这个电话号码插入PHP中的MySQL数据库呢

如何创建一个带有电话号码数字分隔符的输入元素,并将其插入MySQL数据库

以下是HTML代码:


表,td{
边界塌陷:塌陷;
填充:0;
边框:2件纯黑;
}
td输入{
宽度:20px;
}
var currentDigit=1;
函数isNumberKey(evt){
var charCode=(evt.which)?evt.which:event.keyCode
如果(字符码>31&(字符码<48 | |字符码>57))
返回false;
返回true;
}
函数MOVETON输出数字(e){
currentDigit=parseInt(例如,id.replace(“数字”),“”);
当前数字++;

如果(e.value.length==1&¤tDigit我已经在下面的函数中添加了一行,以包含jQuery
$.post()
。您将找到有关
.post
的更多信息。这将通过将电话号码发送到
insertphonenumber.php
将其添加到数据库中

您需要在页面的头部包含一个jquery链接

<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js">
</script>
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()); 
}

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

您需要在页面的头部包含一个jquery链接

<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js">
</script>
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()); 
}

您能展示一个手机号码格式的示例吗?您可以将完整号码存储在单独的隐藏字段或JS变量中,并通过表单或Ajax将其发送到服务器。您能展示更多关于您的数据库结构和您迄今为止所做的尝试吗?(比如您的php代码/数据库连接/数据库)您必须创建一个AJAX调用,将
phoneNumber
变量作为数据发送到一个php文件,该文件将处理数据库插入。或者,您可以将输入字段放在
标记内,并使用名称[]你能展示你的电话号码格式的一个例子吗?你可以把完整的号码存储在单独的隐藏字段或JS变量中,然后通过表单或AJAX将它发送到服务器。你能展示更多关于你的数据库结构和你迄今为止所做的尝试吗?(就像你的php代码/数据库连接/数据库一样)你必须创建一个AJAX调用,将
phoneNumber
变量作为数据发送到一个php文件,该文件将处理数据库插入。或者你可以将你的输入字段放在
标记内,使用名称[]作为输入字段,并以这种方式发送到一个php文件。