我意识到我的问题可能有点微不足道,但我是SQL / mySQL的新手,并且没有通过谷歌找到我的问题的答案。这是我正在使用的mySQL表。
>>> final_list = list(actions) # copy of `actions` list as you want the desired
# result in the form of new list
>>> for i, cost in zip(tuple_position, costs):
... final_list[i] = cost
...
>>> final_list
[(2, 1, 3), (1, 0, 3), 'lift 4', (2, 1, 3), 'down 2', (1, 2, 3), 'lift 4', 'down 2']
我想要的是一个用于向此表添加新贡献的php文件。新贡献将从之前的现有贡献中获得PreviousCID和NextCID。如何将这些变量添加到我的新贡献中。
提前致谢。
编辑:添加了外键
答案 0 :(得分:0)
首先我认为你应该为PreviousCID和NextCID创建一个外键,以表格贡献本身就像那样:
--
-- Constraints for table `contribution`
--
ALTER TABLE `contribution`
ADD CONSTRAINT `contribution_ibfk_1` FOREIGN KEY (`PreviousCID`) REFERENCES `contribution` (`ContributionID`),
ADD CONSTRAINT `contribution_ibfk_2` FOREIGN KEY (`NextCID`) REFERENCES `contribution` (`ContributionID`);
而不是php我不明白你的方法:
新的贡献将获得之前现有贡献的PreviousCID和NextCID
但我认为这会对你有所帮助:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$existing_contribution_id = $_POST['SOME_VARIBALE']; //Here get existing contribution
//get from previous existing contributions
$query = "SELECT * FROM contribution Where =".$existing_contribution_id ;
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
//insert new contribution
$sql = "INSERT INTO contribution (CreatedByUserId, StoryID, PreviousCID,
NextCID , OriginalCID , Content)VALUES ('some_user_id', 'some_story_id',".
$row['PreviousCID'].",".$row['NextCID'].", 'some_OriginalCID' , 'some_content')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();