首页 \ 问答 \ 需要帮助将默认文本值插入mysql(need help inserting a default text value into mysql)

需要帮助将默认文本值插入mysql(need help inserting a default text value into mysql)

结束网络开发人员,我从另一个团队完成了CMS,我必须与我的前端链接。 我做了一些修改,但由于我缺乏PHP知识,我在这里有一些问题。

我的用户可以填写表单,其中1个文本字段要求他们的照片链接。 我想检查输入的值是否不等于我想要的,然后我将查询插入一个默认的头像照片链接到mysql进行处理。

我在php上试过的代码

// check if the variable $photo is empty, if it is, insert the default image link
if($photo = ""){
    $photo="images/avatarDefault.png";
}

似乎没有用

<?php
if($_SERVER["REQUEST_METHOD"] === "POST")
{
    //Used to establish connection with the database
    include 'dbAuthen.php';
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else
    {

        //Used to Validate User input
        $valid = true;

        //Getting Data from the POST
        $username = sanitizeInput($_POST['username']);
        $displayname = sanitizeInput($_POST['displayname']);
        $password = sanitizeInput($_POST['password']);

        //hash the password using Bcrypt - this is to prevent 
        //incompatibility from using PASSWORD_DEFAULT when the default PHP hashing algorithm is changed from bcrypt  
        $hashed_password = password_hash($password, PASSWORD_BCRYPT);

        //Determining Type of the User
        //if B - User is student
        //if A - User is adin
        if($_POST['type'] == 'true')
            $type = 'B';
        else
            $type = 'A';

        $email = sanitizeInput($_POST['email']);
        $tutorGroup = sanitizeInput($_POST['tutorGroup']);
        $courseID = sanitizeInput($_POST['courseID']);
        $description = sanitizeInput($_POST['desc']);
        $courseYear = date("Y");
        $website = sanitizeInput($_POST['website']);
        $skillSets = sanitizeInput($_POST['skillSets']);
        $specialisation = sanitizeInput($_POST['specialisation']);
        $photo = sanitizeInput($_POST['photo']);

        // this is what i tried, checking if the value entered is empty, but doesn't work
        if($photo = ""){
            $photo="images/avatarDefault.png";
        }

        $resume = sanitizeInput($_POST['resume']);

        //Validation for Username
        $sql = "SELECT * FROM Users WHERE UserID= '$username'";
        if (mysqli_num_rows(mysqli_query($con,$sql)) > 0){
            echo 'User already exists! Please Change the Username!<br>';
            $valid = false;
        }

        if($valid){
            //Incomplete SQL Query
            $sql = "INSERT INTO Users
             VALUES ('$username','$displayname','$hashed_password','$type','$email', '$tutorGroup', ";

            //Conditionally Concatenate Values
            if(empty($courseID))
            {
                $sql = $sql . "NULL";
            }
            else
            {
                $sql = $sql . " '$courseID' ";
            }

            //Completed SQL Query
            $sql = $sql . ", '$description', '$skillSets', '$specialisation', '$website', '$courseYear', '$photo',  '$resume', DEFAULT)";

            //retval from the SQL Query
            if (!mysqli_query($con,$sql))
            {
                echo '*Error*: '. mysqli_error($con);
            }
            else
            {
                echo "*Success*: User Added!";
            }
        }

        //if student create folder for them
        if ($type == 'B')
        {
            //Store current reporting error
            $oldErrorReporting = error_reporting();

            //Remove E_WARNING from current error reporting level to prevent users from seeing code
            error_reporting($oldErrorReporting ^ E_WARNING);

            //Set current reporting error();
            error_reporting($oldErrorReporting);
        }

        mysqli_close($con);
    }
}
function sanitizeInput($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

我试图在mysql上找到一种方法来插入默认值,但似乎不可能,所以我别无选择,只能通过php查询插入。

我有逻辑,但我不知道如何在PHP上实现我缺乏知识,我想要检查1)如果照片链接没有单词.png / .jpg, $photo != ".png" 2)如果照片链接长度太低$.photo.length < 10

有人可以帮助我查看代码并告诉我我做错了什么? 谢谢!


end web developer, i was given a CMS done from another team and i have to link with my front-end. I have made some modifications, but due to my lack of php knowledge i have some issue here.

My users are able to fill up a form, where 1 text field is asking for their photo link. I want to check for if the value entered is not equal to what i want, then i will query insert a default avatar photo link to mysql to process.

code that i tried on php

// check if the variable $photo is empty, if it is, insert the default image link
if($photo = ""){
    $photo="images/avatarDefault.png";
}

doesn't seem to work

<?php
if($_SERVER["REQUEST_METHOD"] === "POST")
{
    //Used to establish connection with the database
    include 'dbAuthen.php';
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else
    {

        //Used to Validate User input
        $valid = true;

        //Getting Data from the POST
        $username = sanitizeInput($_POST['username']);
        $displayname = sanitizeInput($_POST['displayname']);
        $password = sanitizeInput($_POST['password']);

        //hash the password using Bcrypt - this is to prevent 
        //incompatibility from using PASSWORD_DEFAULT when the default PHP hashing algorithm is changed from bcrypt  
        $hashed_password = password_hash($password, PASSWORD_BCRYPT);

        //Determining Type of the User
        //if B - User is student
        //if A - User is adin
        if($_POST['type'] == 'true')
            $type = 'B';
        else
            $type = 'A';

        $email = sanitizeInput($_POST['email']);
        $tutorGroup = sanitizeInput($_POST['tutorGroup']);
        $courseID = sanitizeInput($_POST['courseID']);
        $description = sanitizeInput($_POST['desc']);
        $courseYear = date("Y");
        $website = sanitizeInput($_POST['website']);
        $skillSets = sanitizeInput($_POST['skillSets']);
        $specialisation = sanitizeInput($_POST['specialisation']);
        $photo = sanitizeInput($_POST['photo']);

        // this is what i tried, checking if the value entered is empty, but doesn't work
        if($photo = ""){
            $photo="images/avatarDefault.png";
        }

        $resume = sanitizeInput($_POST['resume']);

        //Validation for Username
        $sql = "SELECT * FROM Users WHERE UserID= '$username'";
        if (mysqli_num_rows(mysqli_query($con,$sql)) > 0){
            echo 'User already exists! Please Change the Username!<br>';
            $valid = false;
        }

        if($valid){
            //Incomplete SQL Query
            $sql = "INSERT INTO Users
             VALUES ('$username','$displayname','$hashed_password','$type','$email', '$tutorGroup', ";

            //Conditionally Concatenate Values
            if(empty($courseID))
            {
                $sql = $sql . "NULL";
            }
            else
            {
                $sql = $sql . " '$courseID' ";
            }

            //Completed SQL Query
            $sql = $sql . ", '$description', '$skillSets', '$specialisation', '$website', '$courseYear', '$photo',  '$resume', DEFAULT)";

            //retval from the SQL Query
            if (!mysqli_query($con,$sql))
            {
                echo '*Error*: '. mysqli_error($con);
            }
            else
            {
                echo "*Success*: User Added!";
            }
        }

        //if student create folder for them
        if ($type == 'B')
        {
            //Store current reporting error
            $oldErrorReporting = error_reporting();

            //Remove E_WARNING from current error reporting level to prevent users from seeing code
            error_reporting($oldErrorReporting ^ E_WARNING);

            //Set current reporting error();
            error_reporting($oldErrorReporting);
        }

        mysqli_close($con);
    }
}
function sanitizeInput($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

i've tried finding a way on mysql to insert default values but it seem impossible, so i have no choice but to query insert through php.

I have the logic but i'm not sure how to implement on the php with my lack of knowledge, i was thinking of checking either 1) if the photo link does not have the word .png/.jpg, $photo != ".png" 2) if the photo link length is too low $.photo.length < 10

can someone help me look into the code and tell me what i'm doing wrong? Thanks!


原文:https://stackoverflow.com/questions/27100528
更新时间:2021-12-28 19:12

最满意答案

在XE7和XE8中如下

  public
    { Public declarations }
    /// <comments>Some comments<para/>comments on a second line</comments>
    procedure SetUp;

在Help Insight弹出窗口的新行中将文本“注释放在第二行”。 一个小怪癖是第二行缩进了几个空格,但如果我这样做

/// <comments>Some comments<para>comments on a second line</para>third line</comments>

“第三行”没有缩进。 通过执行以下操作,可以屏蔽缩进不一致(以将所有内容缩进两个空格为代价):

///<comments><para>Some comments</para><para>comments on a second line</para><para>third</para></comments>
procedure SetUp;

从实验来看,

<p/>

XML标记曾经在XE4中工作,但在XE7中停止工作,就像在我的初始测试中一样:

  TForm1 = class(TForm)
    CDS: TClientDataSet;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    procedure CDSCalcFields(DataSet: TDataSet);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    ///<comments>Some comments<p/>more</comments>
    procedure AddHLIndex;

在XE4中,上面显示了XE4中新行的“more”,但与XE8中的“Some comments”在同一行。

我想知道XE8的差异是否与Castalia的存在有关,但是XE4和XE8之间的区别是XE8与/ NOCASTALIA开关一样。

我没有详尽地测试,但XE8忽略了我尝试过的所有“HTML”格式标签(除了

<c>

提问者提到的标签,当然可能是故意改变或意外的结果。 另一方面,它确实似乎处理HTML转义,如

&gt;

&lt;

但不幸的是,

&#10;

,它只是忽略了。


In XE7 and XE8 the following

  public
    { Public declarations }
    /// <comments>Some comments<para/>comments on a second line</comments>
    procedure SetUp;

puts the text 'comments on a second line' on a new line in the Help Insight pop-up. A minor quirk is the second line is indented a couple of spaces, but if I do

/// <comments>Some comments<para>comments on a second line</para>third line</comments>

the 'third line' isn't indented. The indentation inconsistency can by masked (at the expense of indenting everything by two spaces) by doing:

///<comments><para>Some comments</para><para>comments on a second line</para><para>third</para></comments>
procedure SetUp;

Judging by experiments, the

<p/>

XML tag used to work in XE4, but stopped working by XE7, as in my initial test:

  TForm1 = class(TForm)
    CDS: TClientDataSet;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    procedure CDSCalcFields(DataSet: TDataSet);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    ///<comments>Some comments<p/>more</comments>
    procedure AddHLIndex;

In XE4 the above displays the 'more' on a new line in XE4 but on the same line as 'Some comments' in XE8.

I wondered whether the difference in XE8 was anything to do with the presence of Castalia, but I get the same difference between XE4 and XE8 with XE8 started with the /NOCASTALIA switch.

I haven't tested exhaustively but XE8 ignores all the 'HTML' formatting tags I've tried (except the

<c>

tag mentioned by the questioner), which might be the result of a deliberate change or an accident, of course. On the other hand, it does seem to process HTML escapes such as

&gt;

and

&lt;

but not, unfortunately,

&#10;

, which it just ignores.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。