当前位置: 动力学知识库 > 问答 > 编程问答 >

php - MYSQL insert is not working

问题描述:

Technically it accepts all the input but there no any error message that there is something wrong with my code. The insert is located at the bottom part of the code which is strangely not working because I tried my insert statement in the database and of course changing the variables with real values and it worked that way.

Tried this insert statement in my workbench and it worked:

INSERT INTO members (organization,login,password,name,title,profil,created,logout_time,timezone) VALUES ('1','Kristen','123','Kristen Crooks','Volunteer','2','2016-10-11 14:09','0','0')

Code:

<html>

<head>

<meta charset="UTF-8">

<title></title>

<center><b> Volunteer Management </b></center>

<center>

<a href="index.php">Add User </a>

<a href="add_skill.php"> Add Skills </a>

<a href="assign_user.php">Assign User</a>

</center>

</head>

<body>

<?php

$config = parse_ini_file('/config.ini');

$conn = new mysqli($config['servername'], $config['username'], $config['password'], $config['database']);

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}

$sql = "SELECT skillName FROM skills";

$result = $conn->query($sql);

$num=mysqli_num_rows($result);

$conn->close();

?>

<form method="get">

Username: <br>

<input type="text" name="username"> <br>

Password: <br>

<input type="text" name="password"> <br>

Name: <br>

<input type="text" name="name"> <br>

Title: <br>

<input type="text" name="title"> <br> <br>

Permission:

<select id="permission" name="permission">

<option selected disabled>Choose here</option>

<option value="0">Admin</option>

<option value="1">Project Manager</option>

<option value="2">Volunteer</option>

<option value="6">Accounting Officer</option>

</select> <br><br>

Skills: <br>

<?php

$i=0;while ($row = mysqli_fetch_assoc($result)) {

$skillName=$row['skillName'];

?>

<input type="checkbox" name="skills" value="<?php echo $skillName; ?>" ><?php echo $skillName; ?><br>

<?php $i++;} ?>

<br>

<input type="submit" name="submit">

</form>

<?php

if(isset($_GET['submit']))

{

$config = parse_ini_file('/config.ini');

$conn = new mysqli($config['servername'], $config['username'], $config['password'], $config['database']);

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}

$login = $_GET['username'];

$password = $_GET['password'];

$name = $_GET['name'];

$title = $_GET['title'];

$profil = $_GET['permission'];

$created = date('Y-m-d H:i:s');

$sql = "INSERT INTO members (organization,login,password,name,title,profil,created,logout_time,timezone)

VALUES ('1','$login','$password','$name','$title','$profil','$created','0','0')";

$result = $conn->query($sql) or die('Error, insert query failed');

$conn->close();

}

?>

</body>

网友答案:

Okay I already dissected the code and it appears that the $created = date('Y-m-d H:i:s'); is causing the error. By the time I tried not to include it in the data in the insert statement, it worked! I guess I have to get another way to get the date from the system.

Also I've checked that the reason why there is an error is because the date format is in VARCHAR and not in date and another problem is that it does not fit in the length that set. Although I want to ask why it is not detected by the server that there is an error like that

网友答案:
  1. Check PHP log and mysql log, write what is there.
  2. Put an echo before the INSERT, check if your script comes to that place.

If echo is not working- then the problem is not in INSERT

分享给朋友:
您可能感兴趣的文章:
随机阅读: