一、微信小程序
1、index.js
const app = getApp()
Page({
fortn: function () {
//写入数据库
wx.request({
url: 'http://xxxxxxxxx/post.php',
method: 'GET',
data: {
something1: '李四',
something2: '25',
something3: '0'
},
header: {
'content-Type': 'application/x-www-form-urlencoded'
},
success(res) {
console.log(res.data);
if (res.data.status == 0) {
wx.showToast({
title: '提交失败!!!',
icon: 'loading',
duration: 1500
})
} else {
wx.showToast({
title: '提交成功!!!',
icon: 'success',
duration: 1000
})
}
}
});
},
})
2、index.wxml
<button class="ff" bindtap='fortn' >增加</button>
--------------------------------------------------------------
二、服务器端
1、connect.php
<?php
//header("Content-type: text/html; charset=utf8");
//1. 声明字符编码
$host='127.0.0.1';//数据库ip
$user='root';//用户名
$password='123456';//密码
$dbName='mymy';//要连接的数据库名
$con =new mysqli($host,$user,$password,$dbName,3308);//数据库连接
/*if ($con->connect_error) {
echo "系统异常,连接数据库失败";
}
else
{
echo "连接成功";
}*/
?>
2、post.php
<?php
//header("Content-type: text/html; charset=utf8");
include 'connect.php';//调用connect.php文件
$something1=$_GET['something1'];//'小明';//
$something2=$_GET['something2'];//'16';//
$something3=$_GET['something3'];//'0';//
if ($con->connect_error) {
die("连接失败:".$con->connect_error);
}
else
{
$sql="INSERT INTO `doctor`(`name`, `age`, `xb`) VALUES ('$something1','$something2','$something3');";
$res=$con->query($sql);
if($res){
$arr['status'] = 1;
$arr['info'] = 'success';
}else{
$arr['status'] = 0;
$arr['info'] = 'error';
}
echo json_encode($arr);
die;
}
?>
---------------------------------------------------
三、mysql数据库名称为:mymy,表单是doctor,字段名称为`name`, `age`, `xb`
-----------------------------------------------------------------------------
增加数据不成功,不知为什么?(服务器http服务能访问到)
现在服务器上使用127.0.0.1,root,123456看是否能连上mysql吧
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: something1 in C:\wamp\www\post.php on line <i>4</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0002</td><td bgcolor='#eeeeec' align='right'>132744</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\post.php' bgcolor='#eeeeec'>...\post.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: something2 in C:\wamp\www\post.php on line <i>5</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0002</td><td bgcolor='#eeeeec' align='right'>132744</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\post.php' bgcolor='#eeeeec'>...\post.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: something3 in C:\wamp\www\post.php on line <i>6</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0002</td><td bgcolor='#eeeeec' align='right'>132744</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\post.php' bgcolor='#eeeeec'>...\post.php<b>:</b>0</td></tr>
</table></font>
{"status":0,"info":"error"}
数据库ip是127.0.0.1吗?是外网地址,还是内网地址?
挨着排查,数据库连接,数据库,数据库字段,数据库端口,看看都对应上了没